【问题标题】:How to add missing column data with 0 counts on Pandas DataFrame?如何在 Pandas DataFrame 上添加计数为 0 的缺失列数据?
【发布时间】:2018-06-02 21:51:06
【问题描述】:

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

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

[year=2018, week=6, day of week=1, hour=1, count=0, unit_id=blah, unit_label=blah]

[year=2018, week=6, day of week=1, hour=2, count=0,unit_id=blah, unit_label=blah]

...

[year=2018, week=6, day of week=1, hour=23, count=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}}

【问题讨论】:

  • 请将您的数据发布为文本,而不是图片。
  • 如何在此处以视觉上令人愉悦的方式将此 data.head() 结果作为文本输入?我认为包含图片会更容易查看。
  • 图片不能轻易复制。如果这是在 jupyter notebook 中,只需复制文本并直接粘贴即可。甚至可以让我们生成几行的代码
  • 这就是我的意思。我试着这样做,一切都很混乱。我发帖时有一些表格设置吗?
  • 试试df.head(5).to_dict() 这样我们就可以重现它了。

标签: python pandas


【解决方案1】:

我相信这应该适合你。它将为从最小日期到最大日期的每一小时创建一个数据框(非常大!),并且每个小时都有一个条目,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  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 2010-09-25
    • 2022-11-25
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多