一、文件读取与写入
1、文件读取
pandas的文件读数功能较多,可以读多种格式的数据:
一般情况下会读取csv,excel,txt文件。
读取本地文件:
file=pandas.read_csv('/Users/l/Desktop/sample.csv')
print(file.head())
读取txt文件:
df=pd.read_table('/Users/l/Desktop/result3.txt',sep='\t',names=['ap_categories','ssid','bssid','uuid_count','time'])#可以添加间隔符sep,列名
读取Excel文件:
file = pandas.read_excel('/Users/l/Desktop/user_ap_bs_poi/all.xlsx',nrows=200)#读取200行数据
读取Excel的速度会比CSV文件慢一些,可能与Excel文件有多个sheet有关.
读文件时的参数:
header=None 表示第一行不作为列名
index_col 表示把某一列或几列作为索引
usecols 表示读取列的集合,默认读取所有的列
parse_dates 表示需要转化为时间的列
nrows 表示读取的数据行数
文件写入:
我们处理完的数据需要保存为新的数据,需要写到笨地文件夹中:
pivot_file.to_csv('/Users/l/Desktop/数据验证/pivot_table.csv')
将数据中的行索引去除:
pivot_file.to_csv('/Users/l/Desktop/数据验证/pivot_table.csv',index=False)
保存为txt:
pivot_file.to_csv('/Users/l/Desktop/数据验证/pivot_table.txt',sep='\t',index=False)#用csv的方式,定义分隔符
转换为markdown或者latex:
先安装tabulate包:
import pandas
data=pandas.read_csv('/Users/l/Desktop/test.csv')
print(data.to_markdown())
print(data.to_latex())
2、基本数据结构
Series和Dataframe
series:
1/data
2/index
3/dtyp
4/name
a=pandas.Series(data=[1,2,'q',[4,5],{'name':'chen'}],index=[1,2,3,4,5],
dtype='object',name='chen')
print(a)
1 1
2 2
3 q
4 [4, 5]
5 {'name': 'chen'}
Name: chen, dtype: object
可以通过.的方式获取series的属性:
print(a.values)
print(a.index)
print(a.dtype)
print(a.name)
[1 2 'q' list([4, 5]) {'name': 'chen'}]
Int64Index([1, 2, 3, 4, 5], dtype='int64'