合并 pandas 数据框列表 [英] Merge a list of pandas dataframes

查看:89
本文介绍了合并 pandas 数据框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多类似的问题,但没有专门针对这个问题的.

There has been many similar questions but none specifically to this.

我有一个数据帧列表,我需要使用唯一列(date)将它们合并在一起.字段名称不同,因此concat退出了.

I have a list of data frames and I need to merge them together using a unique column (date). Field names are different so concat is out.

我可以手动使用df[0].merge(df[1],on='Date').merge(df[3],on='Date)等将每个df逐一合并,但是问题是列表中的数据帧数随用户输入而不同.

I can manually use df[0].merge(df[1],on='Date').merge(df[3],on='Date) etc. to merge each df one by one, but the issue is that the number of data frames in the list differs with user input.

是否有任何一种合并方式可以一次性将列表中的所有数据帧合并在一起?还是有一些for in loop这样做的?

Is there any way to merge that just combines all data frames in a list at one go? Or perhaps some for in loop at does that?

我正在使用Python 2.7.

I am using Python 2.7.

推荐答案

您可以使用reduce函数,其中dfList是您的数据帧列表:

You can use reduce function where dfList is your list of data frames:

import pandas as pd
from functools import reduce
reduce(lambda x, y: pd.merge(x, y, on = 'Date'), dfList)

作为演示:

df = pd.DataFrame({'Date': [1,2,3,4], 'Value': [2,3,3,4]})
dfList = [df, df, df]
dfList

# [   Date  Value
#  0     1      2
#  1     2      3
#  2     3      3
#  3     4      4,    Date  Value
#  0     1      2
#  1     2      3
#  2     3      3
#  3     4      4,    Date  Value
#  0     1      2
#  1     2      3
#  2     3      3
#  3     4      4]

reduce(lambda x, y: pd.merge(x, y, on = 'Date'), dfList)
#   Date  Value_x  Value_y  Value
# 0    1        2        2      2
# 1    2        3        3      3
# 2    3        3        3      3
# 3    4        4        4      4

这篇关于合并 pandas 数据框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆