2 回答
TA贡献1821条经验 获得超4个赞
glob这是使用和 的解决方案groupby+last:
import glob
list_of_files = glob.glob(r'folder_path\*.csv')
dfs = [pd.read_csv(i) for i in list_of_files]
output = pd.concat(dfs).groupby("Item_id",as_index=False).last()
Item_id Country Compay
0 1 Germany sitz.co
1 2 Russia likrds.co
2 3 Denmark Axitr.co
3 4 Italy ghrx.co
4 11 Argentina sitz.co
5 13 Denmark Axitr.co
如果你只有 2 个数据框:
l = [df1,df2]
output = pd.concat(l).groupby("Item_id",as_index=False).last()
TA贡献1877条经验 获得超6个赞
用于设置和DataFrame.set_index
的索引,然后使用:df1
df2
Item_id
DataFrame.combine_first
df = df2.set_index('Item_id')\
.combine_first(df1.set_index('Item_id')).reset_index()
结果:
print(df)
Item_id Country Compay
0 1 Germany sitz.co
1 2 Russia likrds.co
2 3 Denmark Axitr.co
3 4 Italy ghrx.co
4 11 Argentina sitz.co
5 13 Denmark Axitr.co
添加回答
举报