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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With