导入csv文件
from pandas import read_csv
df=read_csv('/Users/cuiwenhao/Data_xxx/4.1/1.csv')
print(df)
age name
0 23 KEN
1 32 John
2 25 JIMI
导入文本文件
#原文本
23,KEN
32,John
25,JIMI
中文,英文
from pandas import read_table
#没有传names,默认第一行为表头
df=read_table('/Users/cuiwenhao/Data_xxx/4.1/2.txt')
print(df)
23,KEN
0 32,John
1 25,JIMI
2 中文,英文
from pandas import read_table
df=read_table('/Users/cuiwenhao/Data_xxx/4.1/2.txt',names=['age','name'],sep=',')
print(df)
age name
0 23 KEN
1 32 John
2 25 JIMI
3 中文 英文
导入Excel文件(xls,xlsx)
from pandas import read_excel
df=read_excel('/Users/cuiwenhao/Data_xxx/4.1/3.xls',sheet_name='data')
print(df)
age name
0 23 KEN
1 32 John
中文路径问题
from pandas import read_table
filePath = '/Users/cuiwenhao/Data_xxx/4.1/中文.txt'
df1 = read_table(
filePath,
sep=',',
encoding='UTF-8'
)
df2 = read_table(
filePath,
sep=',',
encoding='UTF-8',
engine='python'
)
c1 c2
0 中文1 中文2
c1 c2
0 中文1 中文2
数据导出
from pandas import DataFrame;
df = DataFrame({
'age': [21, 22, 23],
'name': ['KEN', 'John', 'JIMI']
})
print(df)
print(df.to_csv(
"/Users/cuiwenhao/Data_xxx/4.2/1"
))
#j结果有‘,’
,age,name
0,21,KEN
1,22,John
2,23,JIMI
#传入index参数
df.to_csv(
"/Users/cuiwenhao/Data_xxx/ddd",
index=False
)
age,name
21,KEN
22,John
23,JIMI