Pandas系列-读取csv/txt/excel/mysql数据

本代码演示:

  1. pandas读取纯文本文件
  • 读取csv文件
  • 读取txt文件
  1. pandas读取xlsx格式excel文件
  2. pandas读取mysql数据表
import pandas as pd

1、读取纯文本文件

1.1 读取CSV,使用默认的标题行、逗号分隔符

fpath = "./datas/ml-latest-small/ratings.csv"
# 使用pd.read_csv读取数据
ratings = pd.read_csv(fpath)
# 查看前几行数据
ratings.head()
userIdmovieIdratingtimestamp
0114.0964982703
1134.0964981247
2164.0964982224
31475.0964983815
41505.0964982931
# 查看数据的形状,返回(行数、列数)
ratings.shape
(100836, 4)
# 查看列名列表
ratings.columns
Index(['userId', 'movieId', 'rating', 'timestamp'], dtype='object')
# 查看索引列
ratings.index
RangeIndex(start=0, stop=100836, step=1)
# 查看每列的数据类型
ratings.dtypes
userId         int64
movieId        int64
rating       float64
timestamp      int64
dtype: object

1.2 读取txt文件,自己指定分隔符、列名

fpath = "./datas/crazyant/access_pvuv.txt"
pvuv = pd.read_csv(
    fpath,
    sep="\t",
    header=None,
    names=['pdate', 'pv', 'uv']
)
pvuv
pdatepvuv
02019-09-1013992
12019-09-09185153
22019-09-0812359
32019-09-076540
42019-09-0615798
52019-09-05205151
62019-09-04196167
72019-09-03216176
82019-09-02227148
92019-09-0110561

2、读取excel文件

fpath = "./datas/crazyant/access_pvuv.xlsx"
pvuv = pd.read_excel(fpath)
pvuv
日期PVUV
02019-09-1013992
12019-09-09185153
22019-09-0812359
32019-09-076540
42019-09-0615798
52019-09-05205151
62019-09-04196167
72019-09-03216176
82019-09-02227148
92019-09-0110561

3、读取MySQL数据库

import pymysql
conn = pymysql.connect(
        host='127.0.0.1',
        user='root',
        password='12345678',
        database='test',
        charset='utf8'
    )
mysql_page = pd.read_sql("select * from crazyant_pvuv", con=conn)
mysql_page
pdatepvuv
02019-09-1013992
12019-09-09185153
22019-09-0812359
32019-09-076540
42019-09-0615798
52019-09-05205151
62019-09-04196167
72019-09-03216176
82019-09-02227148
92019-09-0110561

本文的代码地址:https://github.com/peiss/ant-learn-pandas

    原文作者:乒乓球鸡蛋
    原文地址: https://www.cnblogs.com/crazyant/p/11525230.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞