如何按列和索引连接 Pandas DataFrames?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44064299/
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-09-14 03:38:02  来源:igfitidea点击:

How can I concatenate Pandas DataFrames by column and index?

pythonpandas

提问by user2950747

I've got four Pandas DataFrames with numerical columns and indices:

我有四个带有数字列和索引的 Pandas DataFrame:

A = pd.DataFrame(data={"435000": [9.792, 9.795], "435002": [9.825, 9.812]}, index=[119000, 119002])
B = pd.DataFrame(data={"435004": [9.805, 9.783], "435006": [9.785, 9.78]}, index=[119000, 119002])
C = pd.DataFrame(data={"435000": [9.778, 9.743], "435002": [9.75, 9.743]}, index=[119004, 119006])
D = pd.DataFrame(data={"435004": [9.743, 9.743], "435006": [9.762, 9.738]}, index=[119004, 119006])

enter image description here

在此处输入图片说明

I want to concatenate them into one DataFrame like this, matching on both column names and indices:

我想将它们连接成一个像这样的 DataFrame,同时匹配列名和索引:

enter image description here

在此处输入图片说明

If I try to pd.concatthe four dfs, they are stacked (either above and below, or to the side, depending on axis) and I end up with NaNvalues in the df:

如果我尝试pd.concat使用四个 dfs,它们会堆叠在一起(在上方和下方,或在侧面,具体取决于axis),并且我最终会NaN在 df 中得到值:

result = pd.concat([A, B, C, D], axis=0)

enter image description here

在此处输入图片说明

How can I use pd.concat(or merge, joinetc.) to get the right result?

如何使用pd.concat(或mergejoin等等),以获得正确的结果呢?

采纳答案by jezrael

You need concat in pairs:

您需要成对连接:

result = pd.concat([pd.concat([A, C], axis=0), pd.concat([B, D], axis=0)], axis=1)
print (result)
        435000  435002  435004  435006
119000   9.792   9.825   9.805   9.785
119002   9.795   9.812   9.783   9.780
119004   9.778   9.750   9.743   9.762
119006   9.743   9.743   9.743   9.738

Better is stack+ concat+ unstack:

更好的是stack+ concat+ unstack

result = pd.concat([A.stack(), B.stack(), C.stack(), D.stack()], axis=0).unstack()
print (result)
        435000  435002  435004  435006
119000   9.792   9.825   9.805   9.785
119002   9.795   9.812   9.783   9.780
119004   9.778   9.750   9.743   9.762
119006   9.743   9.743   9.743   9.738

More dynamic:

更动态:

dfs = [A,B,C,D]
result = pd.concat([df.stack() for df in dfs], axis=0).unstack()
print (result)
        435000  435002  435004  435006
119000   9.792   9.825   9.805   9.785
119002   9.795   9.812   9.783   9.780
119004   9.778   9.750   9.743   9.762
119006   9.743   9.743   9.743   9.738

回答by ayhan

You can use join too:

您也可以使用 join:

pd.concat((A.join(B), C.join(D)))
Out: 
        435000  435002  435004  435006
119000   9.792   9.825   9.805   9.785
119002   9.795   9.812   9.783   9.780
119004   9.778   9.750   9.743   9.762
119006   9.743   9.743   9.743   9.738