将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