python-7-pandas将字典写入excel文件中

(1)代码
# coding: utf-8
import pandas as pd

result = [{"name":"a1","age":10},{"name":"a2","age":20}]
pf = pd.DataFrame(result)
order = ["age", "name"]  # 指定列的顺序
pf = pf[order]
file_path = pd.ExcelWriter('person.xlsx')  # 打开excel文件
# 替换空单元格
pf.fillna(' ', inplace=True)
# 输出
pf.to_excel(file_path, encoding='utf-8', index=False,sheet_name="sheet1")
file_path.save()

(2)结果

python-7-pandas将字典写入excel文件中_第1张图片

 

 

 

 

你可能感兴趣的:(python3,python)