Pandas 读取数据
本代码演示:
Pandas 读取纯文本文件
- 读取 csv 文件
- 读取 txt 文件
- Pandas 读取 xlsx 格式 excel 文件
- Pandas 读取 mysql 数据表
import pandas as pd
读取纯文本文件
读取 csv,使用默认的标题行、逗号分隔符
fpath = "./datas/ml-latest-small/ratings.csv"
ratings = pd.read_csv(fpath) # 使用 pd.read_csv 读取数据
ratings.head() # 查看前几行数据
userId | movieId | rating | timestamp | |
---|---|---|---|---|
0 | 1 | 1 | 4.0 | 964982703 |
1 | 1 | 3 | 4.0 | 964981247 |
2 | 1 | 6 | 4.0 | 964982224 |
3 | 1 | 47 | 5.0 | 964983815 |
4 | 1 | 50 | 5.0 | 964982931 |
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
读取 txt 文件,自己指定分隔符、列名
fpath = "./datas/crazyant/access_pvuv.txt"
pvuv = pd.read_csv(
fpath,
sep="\t",
header=None,
names=['pdate', 'pv', 'uv']
)
pvuv
pdate | pv | uv | |
---|---|---|---|
0 | 2019-09-10 | 139 | 92 |
1 | 2019-09-09 | 185 | 153 |
2 | 2019-09-08 | 123 | 59 |
3 | 2019-09-07 | 65 | 40 |
4 | 2019-09-06 | 157 | 98 |
5 | 2019-09-05 | 205 | 151 |
6 | 2019-09-04 | 196 | 167 |
7 | 2019-09-03 | 216 | 176 |
8 | 2019-09-02 | 227 | 148 |
9 | 2019-09-01 | 105 | 61 |
读取 excel 文件
fpath = "./datas/crazyant/access_pvuv.xlsx"
pvuv = pd.read_excel(fpath)
pvuv
日期 | PV | UV | |
---|---|---|---|
0 | 2019-09-10 | 139 | 92 |
1 | 2019-09-09 | 185 | 153 |
2 | 2019-09-08 | 123 | 59 |
3 | 2019-09-07 | 65 | 40 |
4 | 2019-09-06 | 157 | 98 |
5 | 2019-09-05 | 205 | 151 |
6 | 2019-09-04 | 196 | 167 |
7 | 2019-09-03 | 216 | 176 |
8 | 2019-09-02 | 227 | 148 |
9 | 2019-09-01 | 105 | 61 |
读取 mysql 数据表
import pymysql
conn = pymysql.connect(
host='127.0.0.1',
user='root',
password='123456',
database='test_db',
charset='utf8'
)
mysql_page = pd.read_sql("select * from crazyant_pvuv", con=conn)
mysql_page
pdate | pv | uv | |
---|---|---|---|
0 | 2019-09-10 | 139 | 92 |
1 | 2019-09-09 | 185 | 153 |
2 | 2019-09-08 | 123 | 59 |
3 | 2019-09-07 | 65 | 40 |
4 | 2019-09-06 | 157 | 98 |
5 | 2019-09-05 | 205 | 151 |
6 | 2019-09-04 | 196 | 167 |
7 | 2019-09-03 | 216 | 176 |
8 | 2019-09-02 | 227 | 148 |
9 | 2019-09-01 | 105 | 61 |