Excel 文件的读写:
安装 matplotlib:
conda install pandas
Excel 读的库: rd 可以看成 read
conda install xlrd
Excel 写的库:wt 可以看成 write
conda install xlwt
Excel 文件内容:
代码:
from __future__ import print_function
import pandas as pd
from pandas import read_excel
pd.set_option("display.max_columns", 4)
pd.set_option("display.max_rows", 6)
df = read_excel("./工作簿1.xlsx", "工作表1")
print(df)
df = read_excel("./工作簿1.xlsx", "工作表1", index_col=0, skiprows=3)
index_col:告诉程序哪一行是序号行,可以隐藏
skiprows:表示跳过 Excel 开头的几行
如果一个 Excel 里面有多个 Sheet ,则可以使用下面的方法来打开:
from __future__ import print_function
import pandas as pd
from pandas import read_excel
# 使用上下文管理器打开 Excel 文件
with pd.ExcelFile("./工作簿1.xlsx") as xls:
for x in range(1, 3):
df = read_excel(xls, "工作表{}".format(x), index_col=0, skiprows=0)
print(df)
写 Excel 文件
import pandas as pd
df = pd.DataFrame([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], index=[0, 1, 2], columns=list("ABCD"))
df.to_excel("./hello.xls")