导入csv文件。文件内容如图
通过pandas读取,进行初步的处理
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author's_name_is_NIKOLA_SS
import pandas as pd #导入包
#1读取csv,使用默认的标题行、逗号分割
# fpath = '要打开文件的路径'
fpath = 'D:\Zsss\\xcccvppo.csv'
ratings = pd.read_csv(fpath) #使用pd.read_csv读取数据
ratings.head() #查看前几行(默认5行)
ratings.shape #查看数据的形状,返回(行数、列数)
ratings.columns # 查看列名列表
ratings.index #查看索引列
ratings.dtypes #查看每一列的数据类型
print(ratings.shape)
print(ratings.head(3))#返回前三行
print(ratings.columns)
print(ratings.dtypes )
print(ratings.head(-3))#返回第一行至倒数3行
输出结果如图:
(7, 3)
num cmk myy
0 1 150 645
1 2 200 745
2 3 250 845
Index(['num', 'cmk', 'myy'], dtype='object')
num int64
cmk int64
myy int64
dtype: object
num cmk myy
0 1 150 645
1 2 200 745
2 3 250 845
3 4 300 950
Process finished with exit code 0
同理,读取txt类型的文件csv类似。
导入txt文件如图:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author's_name_is_NIKOLA_SS
import pandas as pd #导入包
fpath = 'D:\Zsss\cvwexxd.txt'
pvuv = pd.read_csv(
fpath,
sep = '\t', #l列的分隔符
header = None,
names = ['pdate','pv','uv']
)
print(pvuv)
效果如图:
pdate pv uv
0 num,cmk,myy NaN NaN
1 1,150,645 NaN NaN
2 2,200,745 NaN NaN
3 3,250,845 NaN NaN
4 4,300,950 NaN NaN
5 5,400,1545 NaN NaN
6 6,550,1720 NaN NaN
7 7,800,2020 NaN NaN
Process finished with exit code 0