简体   繁体   English

将pandas数据帧压缩到新的数据帧中

[英]Zip pandas dataframes into a new dataframe

I have 2 dataframes: 我有2个数据帧:

df_A DF_A

   country_codes
0              4
1              8
2             12
3             16
4             24

and df_B 和df_B

   continent_codes
0                4
1                3
2                5
3                6
4                5

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. 我想连接这两个,但由于不是所有的值都很常见,我得到了很多NaN。 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)

Source 资源

Output: 输出:

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

From the comments: 来自评论:

I feel like this is too simple, but may I suggest: 我觉得这太简单了,但我可以建议:

df_A['continent_codes'] = df_B['continent_codes']
print(df_A)

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫数据框-具有重组数据的新数据框 - Pandas Dataframes - new dataframe with reorganized data 熊猫数据框:通过查找和计算现有数据框来制作新数据框 - pandas dataframe: make new dataframe from lookup and computation of existing dataframes 如何压缩两个熊猫数据框 - How to zip two pandas dataframes Pandas数据框基于其他数据框的列创建新列 - Pandas dataframe create a new column based on columns of other dataframes 比较两个数据框以使用Pandas返回新数据框-Python - Comparing two dataframes to return a new dataframe using pandas - Python 通过对现有数据框应用逻辑来创建新的 Pandas 数据框 - Creating a new pandas dataframe by applying logic to already existing dataframes 将 Pandas 数据帧行拆分为搜索的列值到新的数据帧中 - Split pandas dataframe rows up to searched column value into new dataframes Pandas 通过在不使用 iterrows 的情况下查询其他数据帧来创建新的 dataframe - Pandas create new dataframe by querying other dataframes without using iterrows 选择具有两个唯一标识符的 pandas dataframe 中的行并将它们存储为新数据帧 - Selecting rows in a pandas dataframe with two unique identifiers and storing these as new dataframes 通过从其他数据框中提取列来创建新的熊猫数据框-ValueError - Creating new pandas dataframe by extracting columns from other dataframes - ValueError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM