繁体   English   中英

如何在Pandas DataFrame上添加计数为0的缺失列数据?

[英]How to add missing column data with 0 counts on Pandas DataFrame?

我有一个看起来像这样的Pandas DataFrame:

我的框架

这是数据集的问题:如果计数为0,则永远不会在提供给我的csv文件中创建该行。 因此,例如,第6周只有2个条目(仅2小时计数)。 我希望第6周有168个条目(因为1周有168小时),其中166个条目将具有0个计数。 所以应该有这样的行:

[年= 2018,星期= 6,星期几= 1,一天中的小时= 1,计数= 0,unit_id = blah,unit_label = blah]

[年= 2018,星期= 6,星期几= 1,一天中的小时= 2,计数= 0,unit_id = blah,unit_label = blah]

...

[年= 2018,星期= 6,星期几= 1,一天中的小时= 23,计数= 1,unit_id = blah,unit_label = blah]

...

等等等等。 从环顾四周,我猜测我需要以某种方式使用“重新索引”。 但是考虑到我想要这些非常具体的列,我不能直接使用日期范围。 任何建议表示赞赏。

数据为文本:

{'count': {0: 5, 1: 1, 2: 1, 3: 8, 4: 1},'day_of_week': {0: 4, 1: 5, 2: 4, 3: 3, 4: 3},'hour_of_day': {0: 23, 1: 0, 2: 18, 3: 19, 4: 21},'unit_id': {0: 'bc9b8ac4-3c57-4fe1-9085-0e3d0b6233d6',1: 'bc9b8ac4-3c57-4fe1-9085-0e3d0b6233d6',2: '7a1efb1d-d4c1-47e1-9320-ff5707eae91e',3: '7a1efb1d-d4c1-47e1-9320-ff5707eae91e',4: '7a1efb1d-d4c1-47e1-9320-ff5707eae91e'},'unit_label': {0: '_debug TestPA',1: '_debug TestPA',2: '_TEMPORARILY_DISABLED_Jenn`s Favorite Destinations',3: '_TEMPORARILY_DISABLED_Jenn`s Favorite Destinations',4: '_TEMPORARILY_DISABLED_Jenn`s Favorite Destinations'},'week': {0: 29, 1: 29, 2: 46, 3: 51, 4: 51},'year': {0: 2017, 1: 2017, 2: 2015, 3: 2015, 4: 2015}}

我相信这应该为您工作。 它将创建一个数据框,从最小日期到最大日期,每一小时每一行都有一行(是如此之大!),并且每个小时都有一个条目, count设置为0

# Start by creating a datetime column in your dataframe:
df['datetime'] = pd.to_datetime(df[['year', 'week', 'day_of_week', 'hour_of_day']]
               .apply(lambda x: '-'.join(x.astype('str')),
                      axis=1), format='%Y-%W-%w-%H')

#use reindex, to reindex hourly
new_df = (df.set_index('datetime')
          .reindex(pd.date_range(min(df.datetime), max(df.datetime), freq='H')))

# Go through and fill all your date and time column as necessary
new_df['week'] = new_df.index.week - 1
new_df['day_of_week'] = new_df.index.dayofweek + 1
new_df['year'] = new_df.index.year
new_df['hour_of_day'] = new_df.index.hour

# next, fill NaN in count with 0, and forward fill in unit id and unit label
new_df['count'].fillna(0, inplace=True)
new_df[['unit_id', 'unit_label']] = new_df[['unit_id', 'unit_label']].fillna(method='ffill')

然后,如果需要,可以删除datetime索引:

new_df.reset_index(drop=True, inplace=True)

>>> new_df.head()
   count  day_of_week  hour_of_day                               unit_id  \
0    1.0            4           18  7a1efb1d-d4c1-47e1-9320-ff5707eae91e   
1    0.0            4           19  7a1efb1d-d4c1-47e1-9320-ff5707eae91e   
2    0.0            4           20  7a1efb1d-d4c1-47e1-9320-ff5707eae91e   
3    0.0            4           21  7a1efb1d-d4c1-47e1-9320-ff5707eae91e   
4    0.0            4           22  7a1efb1d-d4c1-47e1-9320-ff5707eae91e   

                                          unit_label  week  year  
0  _TEMPORARILY_DISABLED_Jenn`s Favorite Destinat...    46  2015  
1  _TEMPORARILY_DISABLED_Jenn`s Favorite Destinat...    46  2015  
2  _TEMPORARILY_DISABLED_Jenn`s Favorite Destinat...    46  2015  
3  _TEMPORARILY_DISABLED_Jenn`s Favorite Destinat...    46  2015  
4  _TEMPORARILY_DISABLED_Jenn`s Favorite Destinat...    46  2015  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM