pandas读取csv,txt,excel文件

注:

  1. 此文章只是记录本人在Bilibili学习pandas的过程
  2. B站视频:https://www.bilibili.com/video/BV1UJ411A7Fs?p=1
  3. 数据来源:https://github.com/peiss/ant-learn-pandas
  4. 编写代码环境为anaconda+ jupyter

准备工作 导包

import pandas as pd

读取csv文件

pandas的 read_excel() 方法

fpath = './datas/ml-latest-small/ratings.csv'
ratings = pd.read_csv(fpath)

查看DataFrame的具体结构

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')

index

ratings.index # 索引
RangeIndex(start=0, stop=100836, step=1)
ratings.dtypes  # 每个列名对应的数据类型
userId         int64
movieId        int64
rating       float64
timestamp      int64
dtype: object

读取txt文件

# 设置txt文件的存储位置
fpath = './datas/crazyant/access_pvuv.txt'

同样调用read_csv()方法

  1. 设置切割对象为 \t
  2. 没有头部
  3. 设置列名为 a,b,c
txt_demo = pd.read_csv(
    fpath,
    sep='\t',
    header=None,
    names=['a','b','c']
)
txt_demo.head()
a b c
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

读取xls文件

fpath = './datas/crazyant/access_pvuv.xlsx'

pandas的 read_excel() 方法

xlsx_demo = pd.read_excel(fpath)
xlsx_demo
日期 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
posted @ 2020-06-10 21:07  Adam_lxd  阅读(364)  评论(0编辑  收藏  举报