[英]python : Pandas - Add missing dates to dataframe
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'. 我只需要填写该月的第一天[第一天]。没有数据的地方,我都需要用“ 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. 我需要以下面的方式输出。
Create a multiindex from combination of unman and date range and reindex the data 通过unman和日期范围的组合创建多索引并重新索引数据
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.