- 写入excel文件
- 读取Excel文件(默认读取行索引即header=0)
- 读取文件数据,指定行列索引
- 不指定行列索引
- 指定行和列索引
- 专门制定列的格式
当excel文件打开时候,利用pandas访问excel文件会被拒绝 写入excel文件
#使用columns指定列名,使用index指定行名
df_out = pd.DataFrame([('string1', 1,1.2),
('string2', 2,2.2),
('string3', 3,3.2)],columns=['name','value1','value2'])
# 保存成excel
df_out.to_excel('tmp.xlsx')
excel中形式为
tmp1=pd.read_excel('tmp.xlsx')
print(tmp1)
输出为
Unnamed: 0 name value1 value2 0 0 string1 1 1.2 1 1 string2 2 2.2 2 2 string3 3 3.2读取文件数据,指定行列索引 不指定行列索引
tmp2=pd.read_excel('tmp.xlsx',header=None)
print(tmp2)
输出为
0 1 2 3 0 NaN name value1 value2 1 0.0 string1 1 1.2 2 1.0 string2 2 2.2 3 2.0 string3 3 3.2指定行和列索引
tmp3=pd.read_excel('tmp.xlsx',index_col=0,header=0)
print(tmp3)
输出为
name value1 value2 0 string1 1 1.2 1 string2 2 2.2 2 string3 3 3.2专门制定列的格式
tmp4=pd.read_excel('tmp.xlsx', dtype={'Name':str, 'Value':float})
print(tmp4)
输出为
Unnamed: 0 name value1 value2 0 0 string1 1 1.2 1 1 string2 2 2.2 2 2 string3 3 3.2



