【发布时间】:2017-09-01 17:57:54
【问题描述】:
我有两个 pandas 数据框,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
【问题讨论】: