Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas, group by resample and fill missing values with zero

Tags:

pandas

I have the following code

import pandas as pd

data = {'date': ['2014-05-01', '2014-05-02', '2014-05-04', '2014-05-01', '2014-05-03', '2014-05-04'],
        'battle_deaths': [34, 25, 26, 15, 15, 14],
        'group': [1, 1, 1, 2, 2, 2]}

df = pd.DataFrame(data, columns=['date', 'battle_deaths', 'group'])

df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
df = df.sort_index()

I want to have a battle deaths count per group without any gaps in the dates. Something like

            battle_deaths  group
date                            
2014-05-01             34      1
2014-05-01             15      2
2014-05-02             25      1
2014-05-02              0      2 <--added with battle_deaths = 0 to fill the date range
2014-05-03              0      1 <--added
2014-05-03             15      2
2014-05-04             26      1
2014-05-04             14      2

I have tried the following but it doesn't work(because the fillna method does not take a number, but adding it here to show what I want to achieve)

df.groupby(df.group.name).resample('D').fillna(0)

How can I do this with pandas?

like image 548
Can't Tell Avatar asked Jan 02 '23 14:01

Can't Tell


1 Answers

Use Resampler.asfreq with parameter fill_value=0:

df = df.groupby('group').resample('D')['battle_deaths'].asfreq(fill_value=0).reset_index()
print (df)
   group       date  battle_deaths
0      1 2014-05-01             34
1      1 2014-05-02             25
2      1 2014-05-03              0
3      1 2014-05-04             26
4      2 2014-05-01             15
5      2 2014-05-02              0
6      2 2014-05-03             15
7      2 2014-05-04             14
like image 87
jezrael Avatar answered May 26 '23 18:05

jezrael