我有多个格式如下的 excel 文件:
ID | Name | Prop1 | Prop2 | User
来自excel1的数据:
ID | Name | Prop1 | Prop2 | Prop3 | User
1 | test | | | | John
来自 Excel2 的数据:
ID | Name | Prop1 | Prop2 | Prop3 | User
1 | test | a | b | | John
来自 Excel3 的数据:
ID | Name | Prop1 | Prop2 | Prop3 | User
1 | test | | | c | John
我想做的是将这些单元格组合起来。
期望的输出:
ID | Name | Prop1 | Prop2 | Prop3 | User
1 | test | a | b | c | John
如果文件中的单元格为空,而另一个文件中有值,我想替换它。
有什么简单的方法可以做到这一点吗?
谢谢。
最佳答案
您可以通过 glob
创建所有 DataFrame 的列表,最终 df
需要 combine_first
与 reduce :
import glob
from functools import reduce
files = glob.glob('files/*.xlsx')
dfs = [pd.read_excel(fp).set_index(['ID','Name','User']) for fp in files]
df1 = reduce(lambda l,r: pd.DataFrame.combine_first(l,r), dfs)
print (df1)
Prop1 Prop2 Prop3
ID Name User
1 test John a b c
编辑:如果不需要将文件与 NaN
组合,解决方案更简单:
import glob
files = glob.glob('files/*.xlsx')
df = pd.concat([pd.read_excel(fp) for fp in files],ignore_index=True)
关于python - 合并来自多个excel的一张表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49608419/