pandas Python 熊猫:垂直和水平连接

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

Python pandas: concat vertical and horizontal

pythonpandasconcatenation

提问by user27074

I am trying to concat two dataframe, horizontally. df2 contains 2 result variables for every observation in df1.

我正在尝试水平连接两个数据框。df2 包含 df1 中每个观察的 2 个结果变量。

df1.shape 
(242583, 172)
df2.shape
(242583, 2)

My code is:

我的代码是:

Fin = pd.concat([df1, df2], axis= 1)

But somehow the result is stacked in 2 dimensions:

但不知何故,结果以二维方式堆叠:

Fin.shape
(485166, 174)

What am I missing here?

我在这里错过了什么?

回答by jezrael

There are different index values, so indexes are not aligned and get NaNs:

有不同的索引值,所以索引没有对齐,得到NaNs:

df1 = pd.DataFrame({
    'A': ['a','a','a'],
    'B': range(3)
})
print (df1)
   A  B
0  a  0
1  a  1
2  a  2

df2 = pd.DataFrame({
    'C': ['b','b','b'],
    'D': range(4,7)
}, index=[5,7,8])
print (df2)
   C  D
5  b  4
7  b  5
8  b  6


Fin = pd.concat([df1, df2], axis= 1)
print (Fin)
     A    B    C    D
0    a  0.0  NaN  NaN
1    a  1.0  NaN  NaN
2    a  2.0  NaN  NaN
5  NaN  NaN    b  4.0
7  NaN  NaN    b  5.0
8  NaN  NaN    b  6.0

One possible solution is create default indexes:

一种可能的解决方案是创建默认索引:

Fin = pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], axis= 1)
print (Fin)
   A  B  C  D
0  a  0  b  4
1  a  1  b  5
2  a  2  b  6

Or assign:

或赋值:

df2.index = df1.index
Fin = pd.concat([df1, df2], axis= 1)
print (Fin)
   A  B  C  D
0  a  0  b  4
1  a  1  b  5
2  a  2  b  6

df1.index = df2.index
Fin = pd.concat([df1, df2], axis= 1)
print (Fin)
   A  B  C  D
5  a  0  b  4
7  a  1  b  5
8  a  2  b  6