多个 pandas 数据框的交集 [英] Intersection of multiple pandas dataframes
问题描述
我在列表中有多个数据帧(100),如下所示:
I have a number of dataframes (100) in a list as:
frameList = [df1,df2,..,df100]
每个数据框都有两列DateTime
,Temperature
.
Each dataframe has the two columns DateTime
, Temperature
.
我要与公共DateTime
列上的所有数据框相交,并将它们的所有Temperature
列组合/合并为一个大数据框:df1的温度,df2的温度,df3的温度,..,df3的温度df100.
I want to intersect all the dataframes on the common DateTime
column and get all their Temperature
columns combined/merged into one big dataframe: Temperature from df1, Temperature from df2, Temperature from df3, .., Temperature from df100.
(熊猫merge
无法正常工作,因为我必须计算多个(99)两两交叉点.)
(pandas merge
doesn't work as I'd have to compute multiple (99) pairwise intersections).
推荐答案
使用pd.concat
,它适用于DataFrame或Series列表.
Use pd.concat
, which works on a list of DataFrames or Series.
pd.concat(frameList, axis=1, join='inner')
这比使用pd.merge
更好,因为pd.merge
每次执行时都会成对复制数据. pd.concat
仅复制一次.但是,pd.concat
仅基于轴合并,而pd.merge
也可以在(多个)列上合并.
This is better than using pd.merge
, as pd.merge
will copy the data pairwise every time it is executed. pd.concat
copies only once. However, pd.concat
only merges based on an axes, whereas pd.merge
can also merge on (multiple) columns.
这篇关于多个 pandas 数据框的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!