python - 在excel中将工作表名称添加到自负的最终合并工作表
问题描述
我想将多个 excel 工作表合并为一个,并使用原始工作表的名称创建一个新列,我使用以下代码:
list_of_sheets = list(df.keys())
cdf = pd.concat(df[sheet] for sheet in list_of_sheets)
# tried
cdf = pd.concat(df[sheet]["Brand"] for sheet in list_of_sheets)
# and
list_of_sheets = list(df.keys())
for sheet in list_of_sheets:
df[sheet]["Brand"] = sheet
cdf = pd.concat(df[sheet])
但它们都不起作用
解决方案
这能实现你想要的吗?
import pandas as pd
pd.concat(pd.read_excel("my_excel_file.xlsx", sheet_name=None))
工作表的名称将是数据框的索引。
推荐阅读
- java - 更改本地时重新创建活动后,Android不会加载假定的字体
- flutter - 使用立方和持续时间更改高度
- java - H2DB 文件中的表不会出现在 IDEA 中,但会显示在 H2 控制台中
- arrays - 从多值属性数组创建对象
- python-3.x - 如何将引用多列的函数应用于熊猫数据框?
- delphi - 无效的类型转换 JSON
- swift - 如何在 Swift 5+ 上使用 FS Calendar CocoaPod 为每个日期添加特定事件
- python - 从文本文件创建数据框
- javascript - Javascript - 如果对象不存在,则将其添加到数组中,否则增加数量
- android - 如何以正确的方式使用 Firebase 的“.info/connected”位置来检查互联网访问?