将Pandas数据框压缩为新的数据框 [英] Zip pandas dataframes into a new dataframe

查看:124
本文介绍了将Pandas数据框压缩为新的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据框:

df_A

   country_codes
0              4
1              8
2             12
3             16
4             24

和df_B

   continent_codes
0                4
1                3
2                5
3                6
4                5

两个数据帧的长度相同,但没有公共列.我想将两者连接起来,但是由于并非所有值都是通用的,因此我得到了很多NaN.如何将它们串联或压缩到组合的数据框中?

Both dataframes have same length, but no common column. I want to concatenate the two but since not all values are common, I get lots of NaNs. How do I concatenate or zip them up into a combined dataframe?

-编辑所需的输出是这样的:

-- EDIT desired output is this:

   country_codes   continent_codes
0              4      4
1              8      3
2             12      5
3             16      6
4             24      5

推荐答案

以下代码将根据您的需要进行操作:

The following code will do as you want :

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

来源

输出:

   country_codes  continent_codes
0              4                4
1              8                3
2             12                5
3             16                6
4             24                5

这篇关于将Pandas数据框压缩为新的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆