enter image description here我有一个名为 Example.xls
的文件,其中标签 sales
和 purchase
中有数据。
我们在 A 列到 E 列的两个选项卡中都有数据。
当我通过 pandas 模块导入这些数据时,我想要像 A 列到 F 列那样的结果,其中 F 列应该显示姓名表名称。如何在pandas模块中显示工作表名称?
我正在使用代码
all= pd.read_excel(Example.xlsx',sheet_name=['销售','采购'])
enter image description here
然后
df= pd.concat(All[frame]for fram in All.keys())
然后我想将选项卡的名称放入数据框“All”中的最后一列,分别为 F
最佳答案
我认为这是最简单的方法。
import pandas as pd
path = r'path_of_your_file'
workbook = pd.read_excel(path, sheet_name = None)
df= pd.DataFrame()
for sheet_name, sheet in workbook.items():
sheet['sheet'] = sheet_name
df = df.append(sheet)
# Reset your index or you'll have duplicates
df = df.reset_index(drop=True)
关于python - 如何导入Excel选项卡并在Python中相应地在新列中给出选项卡的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785991/