I have two pandas data frames, I want to get the sum of items_bought for each ID in DF1. Then add a column to DF2 containing the sum of items_bought calculated from DF1 with matching ID else fill it with 0. How can I do this in an elegant and efficient manner?
DF1
ID | items_bought
1 5
3 8
2 2
3 5
4 6
2 2
DF2
ID
1
2
8
3
2
Desired Result: DF2 Becomes
ID | items_bought
1 5
2 4
8 0
3 13
2 4
df1.groupby('ID').sum().loc[df2.ID].fillna(0).astype(int)
Out[104]:
items_bought
ID
1 5
2 4
8 0
3 13
2 4
ID
. ID
, so you can select with df2
IDs by calling loc
. fillna
. NA
are handled by float type. Now that they are removed, convert the column back to integer. Solution with groupby
and sum
, then reindex
with fill_value=0
and last reset_index
:
df2 = df1.groupby('ID').items_bought.sum().reindex(df2.ID, fill_value=0).reset_index()
print (df2)
ID items_bought
0 1 5
1 2 4
2 8 0
3 3 13
4 2 4
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.