根据条件组合两个大小不等的数据框

时间:2020-01-17 17:53:32

标签: pandas

我有两个数据帧:df1和df2。我想将df2中的列的值添加到df1中。

df1:

Title = ['Aeroplane', 'Ships', 'Houses']
Term = ['Computers', 'Flasks', 'Mouse']
counts_1 = [200, 30, 45, 66, 33, 450, 60, 100, 150]
df_1 = pd.DataFrame({"Title": Title, "Terms": Term})
product_terms = product(term_list, cap_list)
df_1 = pd.DataFrame(product_terms, columns=['Term', 'Title'])
df_1['C1'] = counts_1

       Term        Title       C1
    0  Computers   Aeroplane   200
    1  Computers   Ships        30
    2  Computers   Houses       45
    3  Flasks      Aeroplane    66
    4  Flasks      Ships        33
    5  Flasks      Houses      450
    6  Mouse      Aeroplane     60
    7  Mouse      Ships        100
    8  Mouse      Houses       150

df2(较小的一个)

terms = ['Computers', 'Flasks', 'Flasks', 'Mouse']
title = ['Aeroplane', 'Aeroplane', 'Ships', 'Houses']
count_2 = [3, 6, 13, 15]
df_2 = pd.DataFrame({'Term': terms, 'Title': title, 'C2': count_2})


       Term        Title       C2
    0  Computers   Aeroplane    3
    1  Flasks      Aeroplane    6
    2  Flasks      Ships       13
    3  Mouse      Houses       15

我想将两个df合并为一个df,如下所示:将df_2的列C2添加到df_1(基于Term和Title col),在没有对应的Term和Title cols的地方插入0。 / p>

   Term        Title       C1      C2
0  Computers   Aeroplane   200      3
1  Computers   Ships        30      0
2  Computers   Houses       45      0
3  Flasks      Aeroplane    66      6
4  Flasks      Ships        33     13
5  Flasks      Houses      450      0
6  Mouse      Aeroplane     60      0
7  Mouse      Ships        100     15
8  Mouse      Houses       150      0

df2的术语和标题始终是df1中术语和标题的子集。

这是我尝试过的:

df_1.set_index(['Term', 'Title'], inplace=True)
df_2.set_index(['Term', 'Title'], inplace=True)

然后,遍历行并分配值。

for idx, row in df_1.iterrows():
    try:
        c2_value = df_2.loc[idx, 'C2']
    except:
        df_1.loc[idx, 'C2'] = 0
    else:
        df_1.loc[idx]['C2'] = c2_value


df_final = df_1.reset_index()

是否有更好的方法来实现我想要的?我觉得iterrows可能不是一种有效的方法。我的数据框有数百万行。

0 个答案:

没有答案