繁体   English   中英

在二维中创建两个熊猫数据框的组合

[英]Create combination of two pandas dataframes in two dimensions

我有两个熊猫数据框,df1 和 df2。 我想创建一个数据框 df3,其中包含使用 df1 中的一列和 df2 中的一列的所有组合。 这样做效率低下的伪代码是这样的:

df3 = []
for i in df1:
     for j in df2:
         df3.append(i + j) # where i + j is the row with the combined cols from df1 and df2

这是 df1 的格式:

df1_id    other_data_1    other_data_2
1         0               1
2         1               5

df2:

df2_id    other_data_3    other_data_4
1         0               1
3         2               2

目标是为 df3 获取此输出:

df1_id    df2_id    other_data_1    other_data_2    other_data_3    other_data_4
1         1         0               1               0               1
1         3         0               1               2               2
2         1         1               5               0               1
2         3         1               5               2               2

更新熊猫 1.2.0+

df1.merge(df2, how='cross')

在两个数据帧之间设置一个公共键并使用pd.merge

df1['key'] = 1
df2['key'] = 1

合并和删除键列:

df3 = pd.merge(df1,df2,on='key').drop('key',axis=1)
df3

输出:

   df1_id  other_data_1  other_data_2  df2_id  other_data_3  other_data_4
0       1             0             1       1             0             1
1       1             0             1       3             2             2
2       2             1             5       1             0             1
3       2             1             5       3             2             2

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM