比较两个数据框的列值和增量计数

时间:2019-01-22 07:16:38

标签: python pandas dataframe

我有两个数据框,例如 df1 =

Hair  Feathers  Eggs  Type
 0       0       0     1
 1       0       0     1
 0       1       0     2
 0       1       1     2
 0       0       1     6

和df2 =

Hair  Feathers  Eggs  Count
 0       0       0     
 1       0       0     
 1       0       1     

如何将df2的第一行与df1的所有行进行比较,使得如果df2.Hair等于df1.Hair值,则应通过将Count值增加1来更新Count值。用于df2的第二行,依此类推。 任何帮助将不胜感激。 结果输出应如下所示,仅适用于df2的第一行

Hair  Feathers  Eggs  Type  Count
 0       0       0     1      1
 0       0       0     2      2
 0       0       0     6      1

2 个答案:

答案 0 :(得分:1)

您可以使用groupby进行计数以获取相关计数,然后使用merge将正确的值添加到结果数据框中。例如:

data1 = {'Hair': [0,1,0,0,0],
'Feathers': [0,0,1,1,0],
'Eggs': [0,0,0,1,1],
'Type': [1,1,2,2,6]}
df = pd.DataFrame(data1)

data2 = {'Hair': [0,1,1],
'Feathers': [0,0,0],
'Eggs': [0,0,1]}

df2 = pd.DataFrame(data2)

df_counts = df.groupby(['Hair', 'Type']).agg('count')[['Feathers']].reset_index()
df_counts.rename(columns={'Feathers': 'Count'}, inplace=True)
pd.merge(df2, df_counts, how='outer', on='Hair')

答案 1 :(得分:1)

我相信您需要GroupBy.sizemerge

print (df1.groupby(['Hair', 'Type']).size().reset_index(name='count'))
   Hair  Type  count
0     0     1      1
1     0     2      2
2     0     6      1
3     1     1      1

#if need default inner join
df3 = df2.merge(df1.groupby(['Hair', 'Type']).size().reset_index(name='count'), on='Hair')
#if need right join
#df3 = (df2.merge(df1.groupby(['Hair', 'Type']).size().reset_index(name='count'), 
#                  on='Hair', how='right'))
print (df3)

   Hair  Feathers  Eggs  Type  count
0     0         0     0     1      1
1     0         0     0     2      2
2     0         0     0     6      1
3     1         0     0     1      1
4     1         0     1     1      1