python3指定目录所有excel_根据Python目录中所有Excel文件的多列合并

Say I have a dataframe df, and a directory ./ which has the following excel files inside:

path = './'

for root, dirs, files in os.walk(path):

for file in files:

if file.endswith(('.xls', '.xlsx')):

print(os.path.join(root, file))

# dfs.append(read_dfs(os.path.join(root, file)))

# df = reduce(lambda left, right: pd.concat([left, right], axis = 0), dfs)

Out:

df1.xlsx,

df2.xlsx,

df3.xls

...

I want to merge df with all files from path based on common columns date and city. It works with the following code, but it's not concise enough.

So I raise a question for improving the code, thank you.

df = pd.merge(df, df1, on = ['date', 'city'], how='left')

df = pd.merge(df, df2, on = ['date', 'city'], how='left')

df = pd.merge(df, df3, on = ['date', 'city'], how='left')

...

Reference:

解决方案

The following code may works:

from functools import reduce

dfs = [df0, df1, df2, dfN]

df_final = reduce(lambda left, right: pd.merge(left, right, on=['date', 'city']), dfs)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值