简体   繁体   中英

divide two columns group by two columns

I want to divide two columns revenue from df1 and revenue from df2 grouping by id and date.

I did something like this, but am getting an error.

df.groupby(['id','date']).agg({'ratio': lambda L: df1['revenue'].div(df2['revenue'])})

data

df1 = pd.DataFrame({'id':['x1', 'x2'],
           'date':['2021-01-02',
           '2021-01-03'],
       'revenue':[50,10]})

df2 = pd.DataFrame({'id':['x2', 'x1', 'x1'],
            'date':['2021-01-03','2021-01-02', '2021-01-01'],
       'revenue':[100,100, 200]})

expected output

     id     date    ratio

0   x2  2021-01-03      0.5

1   x1  2021-01-02      0.1

2   x1  2021-01-01      0

Use DataFrame.merge with left join by columns id and date , then divide and replace missing values by 0 :

s = df2.merge(df1, on=['id','date'], how='left')['revenue_y'].div(df2['revenue'])
df2['ratio']  = s.fillna(0)
print (df2)
   id        date  revenue  ratio
0  x2  2021-01-03      100    0.1
1  x1  2021-01-02      100    0.5
2  x1  2021-01-01      200    0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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