Answer the question
In order to leave comments, you need to log in
How to aggregate over a list in pandas, including missing ones?
There is a list of IDs of interest and a bunch of strings with numbers for some, not all of these IDs.
It would be desirable to receive the sum of numbers on each of ID. And zeros for those to which there is none. Something like LEFT OUTER JOIN in SQL.
For example, data:
id x
------
1 10
1 12
2 11
4 21
(1,2,3,4)
. As expected result1 22
2 11
3 0
4 21
df.groupby('id').agg('sum')
Answer the question
In order to leave comments, you need to log in
Suggested on SO : use
reindex ()
my_ids = [1,2,3,4] #для примера
df.groupby('id').sum()
.reindex( my_ids, fill_value=0)
.reset_index()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question