Python Pandas 列绑定(cbind)两个数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33088010/
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
Pandas column bind (cbind) two data frames
提问by breezymri
I've got a dataframe df_a
with id information:
我有一个df_a
带有 id 信息的数据框:
unique_id lacet_number
15 5570613 TLA-0138365
24 5025490 EMP-0138757
36 4354431 DXN-0025343
and another dataframe df_b
, with the same number of rows that I know correspond to the rows in df_a
:
和另一个数据框df_b
,与我知道的行数相同,对应于以下行df_a
:
latitude longitude
0 -93.193560 31.217029
1 -93.948082 35.360874
2 -103.131508 37.787609
What I want to do is simply cbind the two and get:
我想要做的就是简单地将两者绑定并得到:
unique_id lacet_number latitude longitude
0 5570613 TLA-0138365 -93.193560 31.217029
1 5025490 EMP-0138757 -93.948082 35.360874
2 4354431 DXN-0025343 -103.131508 37.787609
What I have tried:
我尝试过的:
df_c = pd.concat([df_a, df_b], axis=1)
which gives me an outer join.
这给了我一个外部连接。
unique_id lacet_number latitude longitude
0 NaN NaN -93.193560 31.217029
1 NaN NaN -93.948082 35.360874
2 NaN NaN -103.131508 37.787609
15 5570613 TLA-0138365 NaN NaN
24 5025490 EMP-0138757 NaN NaN
36 4354431 DXN-0025343 NaN NaN
The problem is that the indices for the two dataframes do not match. I read the documentation for pandas.concat, and saw that there is an option "ignore_index". But that only applies to the concatenation axis, in my case the columns and it certainly is not the right choice for me. So my question is: is there a simple way to achieve this?
问题是两个数据帧的索引不匹配。我阅读了 pandas.concat 的文档,看到有一个选项“ignore_index”。但这仅适用于串联轴,在我的情况下是列,这对我来说肯定不是正确的选择。所以我的问题是:有没有一种简单的方法可以实现这一目标?
采纳答案by EdChum
If you're sure the index row values are the same then to avoid the index alignment order then just call reset_index()
, this will reset your index values back to start from 0
:
如果您确定索引行值相同,那么为了避免索引对齐顺序,只需调用reset_index()
,这会将您的索引值重置为从0
:
df_c = pd.concat([df_a.reset_index(drop=True), df_b], axis=1)