将多个工作表从 Excel 追加到 Pandas 数据帧 - 对问题进行排序



我正在尝试将多个 excel 中的所有工作表附加到熊猫数据帧中。所有工作表都具有相同的列。我需要跳过前 9 行,并在 50 后删除页脚。第 10 行将成为标题。

我写了以下代码:

source_dataset = r"C:locationxxx"
out_df = pd.DataFrame()
for f in source_dataset:
cdf = [pd.read_excel ('excel file name.xlxs',
sheet_name = ['sheet_1',
'sheet_2',
'sheet_3',
'sheet_4',
],
skiprows = 9,
skipfooter = 50).values()
for excel_names in source_dataset]
out_df = pd.concat([pd.concat(x) for x in cdf], ignore_index=True, sort=True)

我收到以下警告:

c:usersxxxappdatalocalprogramspythonpython37libsite-packagesipykernel_launcher.py:35: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass 'sort=False'.
To retain the current behavior and silence the warning, pass 'sort=True'.

我得到的文件不正常,因为一列重复了两次。

1(我做错了什么?

2(我应该在哪里插入排序=假/真?我在这里尝试过,但不起作用:

out_df = pd.concat([pd.concat(x) for x in cdf], ignore_index=True, sort=True)

谢谢

您可以将sort=True传递给两个concat

out_df = pd.concat([pd.concat(x, sort=True) for x in cdf], ignore_index=True, sort=True)

最新更新