侧边栏壁纸
  • 累计撰写 125 篇文章
  • 累计创建 16 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录
Python   

Python_Pandas的excel办公自动化

读取表格

df = pd.read_excel(filepath, index_col=0, skiprows=[0, 1])
  • filepath:文件路径
  • index_col=0:将第一列作为索引
  • skiprows=[0, 1]:在读取时跳过第一二行

筛选行数据

df = df[df['列名'] == "筛选内容"]
df = df[df['列名'] != "去除内容"]

通过该语法可以将包含特定内容的行筛选出来,或者剔除。

创建新表格

newdf = pd.DataFrame(None, None, df.columns)

基于原来的df的数据结构,创建新的表格。

提取唯一值

list = df['列名'].unique()

对一列中的数据提取唯一值列表。

提取行数据

ndf.iloc[num]

提取第几行的数据。

添加新数据

newdf = newdf.append(ndf.iloc[num], ignore_index=False)

输出excel表格

writer = pd.ExcelWriter("./88.xls")  # 初始化一个writer
newdf.to_excel(writer, float_format='%.5f')  # table输出为excel, 传入writer
writer.save()  # 保存
0

评论区