Python 合并熊猫数据框列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38089010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:20:28  来源:igfitidea点击:

Merge a list of pandas dataframes

pythonpandasdataframe

提问by Jake

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

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

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.

我有一个数据框列表,我需要使用唯一的 column 将它们合并在一起(date)。字段名称不同,因此 concat 已出局。

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.

我可以手动使用df[0].merge(df[1],on='Date').merge(df[3],on='Date)etc. 将每个 df 一一合并,但问题是列表中的数据框数量因用户输入而异。

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?

有没有什么方法可以合并一次将所有数据框合并到一个列表中?或者也许有一些 for in 循环呢?

I am using Python 2.7.

我正在使用 Python 2.7。

回答by Psidom

You can use reducefunction where dfListis your list of data frames:

您可以使用reduce函数 wheredfList是您的数据框列表:

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

As a demo:

作为演示:

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