python : Pandas - Add missing dates to dataframe

爱⌒轻易说出口 提交于 2021-02-11 05:34:55

问题


I have the below data. I need to fill in the data for the remaining months

I need only the first day [day one] of the month to be filled in. Wherever there is no data, I need the value to be filled with '0'.

For example below is the existing data

       uname        month_first     msg_count
0     ArtCort0324   2017-06-01      9

I need output in below way.


回答1:


Create a multiindex from combination of unman and date range and reindex the data

df.month_first = pd.to_datetime(df.month_first)

dates = pd.date_range(datetime.datetime(df.month_first.dt.year.min(), 1, 1),datetime.datetime(df.month_first.dt.year.max(), 12, 1), freq = 'MS')

idx = pd.MultiIndex.from_product([df.uname.unique(), dates], names = ['uname','month_first'])

df.set_index(['uname', 'month_first']).reindex(idx).fillna(0).astype(int).reset_index()

uname   month_first msg_count
0   ArtCort0324 2017-01-01  0
1   ArtCort0324 2017-02-01  0
2   ArtCort0324 2017-03-01  0
3   ArtCort0324 2017-04-01  0
4   ArtCort0324 2017-05-01  0
5   ArtCort0324 2017-06-01  9
6   ArtCort0324 2017-07-01  0
7   ArtCort0324 2017-08-01  0
8   ArtCort0324 2017-09-01  0
9   ArtCort0324 2017-10-01  0
10  ArtCort0324 2017-11-01  0
11  ArtCort0324 2017-12-01  0


来源:https://stackoverflow.com/questions/54447119/python-pandas-add-missing-dates-to-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!