1.安装pandes
pip install pandas -i https://mirrors.aliyun.com/pypi/simple/
2.安装openpyxl 务必要安装openpyxl,否则运行代码时会报错
pip install openpyxl -i https://mirrors.aliyun.com/pypi/simple/
3.代码实现(案例):
import pandas as pd
data={
"姓名":["楚枫","楚月","楚狐宇"],
"年龄":["16","17","18"],
"性别":["男","女","男"]
}
df=pd.DataFrame(data)
#显示df
print(df)
#修改索引
df2=pd.DataFrame(data,index=[2,3,4])
#显示df
print(df2)
#存储
df.to_excel(r"D:\pythonProject2\1005\test01.xlsx")#保存为excel文件,前面的r是为了防止后面的\t不转义
运行结果:
生成的excel文件:
打开excel文件:
标签:openpyxl,mirrors,python,步骤,excel,df,pd,data From: https://blog.51cto.com/u_15937426/7711008