Pandas 生成缺失的日期和小时数,值为 0

火焰多摩

我有这个数据框:

date                   station  count
2015-01-01 13:00:00      A        4
2015-01-01 14:00:00      B        2
2015-01-02 15:00:00      A        7

为简单起见,假设该站只有 2 个值:A 和 B

我的目标是为每个日期、每小时和每个站点生成 0 个计数。

例如,代码将生成:

date                   station  count
2015-01-01 00:00:00      A        0
2015-01-01 00:00:00      B        0

这是我试过的:

# generate 0 values (no transaction) for each hour at each station
df_trans = df_trans.set_index(['date', 'station'])

(date_index, station_index) = df_trans.index.levels

# generate a range of all dates & hours
all_dates = pd.date_range('2014-01-09', '2015-12-08', freq='H')

new_index = pd.MultiIndex.from_product([all_dates, station_index])

df_trans = df_trans.reindex(new_index)

df_trans = df_trans['net_rate'].fillna(0)

然而,结果数据帧不是每小时。

输出(日期中没有小时):

               net_rate
2014-01-09 2        0.0
           3        0.0
           4        0.0
耶斯列

对我来说,它的工作不错,小的改进是使用参数fill_value=0reindex

new_index = pd.MultiIndex.from_product([all_dates, station_index], names=('date', 'station'))

df_trans = df_trans.reindex(new_index, fill_value=0)

print (df_trans.head(10))
                             count
date                station       
2014-01-09 00:00:00 A            0
                    B            0
2014-01-09 01:00:00 A            0
                    B            0
2014-01-09 02:00:00 A            0
                    B            0
2014-01-09 03:00:00 A            0
                    B            0
2014-01-09 04:00:00 A            0
                    B            0

print (df_trans[df_trans['count'] != 0])
                             count
date                station       
2015-01-01 13:00:00 A            4
2015-01-01 14:00:00 B            2
2015-01-02 15:00:00 A            7

print (df_trans.index.levels)

[[2014-01-09 00:00:00, 2014-01-09 01:00:00, 2014-01-09 02:00:00, 2014-01-09 03:00:00, 
  2014-01-09 04:00:00, 2014-01-09 05:00:00, 2014-01-09 06:00:00, 2014-01-09 07:00:00, 
  2014-01-09 08:00:00, 2014-01-09 09:00:00, 2014-01-09 10:00:00, 2014-01-09 11:00:00, 
  2014-01-09 12:00:00, 2014-01-09 13:00:00, 2014-01-09 14:00:00, 2014-01-09 15:00:00, 
  2014-01-09 16:00:00, 2014-01-09 17:00:00, 2014-01-09 18:00:00, 2014-01-09 19:00:00, 
  2014-01-09 20:00:00, 2014-01-09 21:00:00, 2014-01-09 22:00:00, 2014-01-09 23:00:00, 
  2014-01-10 00:00:00, 2014-01-10 01:00:00, 2014-01-10 02:00:00, 2014-01-10 03:00:00, 
  2014-01-10 04:00:00, 2014-01-10 05:00:00, 2014-01-10 06:00:00, 2014-01-10 07:00:00, 
  2014-01-10 08:00:00, 2014-01-10 09:00:00, 2014-01-10 10:00:00, 2014-01-10 11:00:00, 
  2014-01-10 12:00:00, 2014-01-10 13:00:00, 2014-01-10 14:00:00, 2014-01-10 15:00:00, 
  2014-01-10 16:00:00, 2014-01-10 17:00:00, 2014-01-10 18:00:00, 2014-01-10 19:00:00, 
  2014-01-10 20:00:00, 2014-01-10 21:00:00, 2014-01-10 22:00:00, 2014-01-10 23:00:00, 
  2014-01-11 00:00:00, 2014-01-11 01:00:00, 2014-01-11 02:00:00, 2014-01-11 03:00:00, 
  2014-01-11 04:00:00, 2014-01-11 05:00:00, 2014-01-11 06:00:00, 2014-01-11 07:00:00, 
  2014-01-11 08:00:00, 2014-01-11 09:00:00, 2014-01-11 10:00:00, 2014-01-11 11:00:00, 
  2014-01-11 12:00:00, 2014-01-11 13:00:00, 2014-01-11 14:00:00, 2014-01-11 15:00:00, 
  2014-01-11 16:00:00, 2014-01-11 17:00:00, 2014-01-11 18:00:00, 2014-01-11 19:00:00, 
  2014-01-11 20:00:00, 2014-01-11 21:00:00, 2014-01-11 22:00:00, 2014-01-11 23:00:00, 
  2014-01-12 00:00:00, 2014-01-12 01:00:00, 2014-01-12 02:00:00, 2014-01-12 03:00:00, 
  2014-01-12 04:00:00, 2014-01-12 05:00:00, 2014-01-12 06:00:00, 2014-01-12 07:00:00, 
  2014-01-12 08:00:00, 2014-01-12 09:00:00, 2014-01-12 10:00:00, 2014-01-12 11:00:00, 
  2014-01-12 12:00:00, 2014-01-12 13:00:00, 2014-01-12 14:00:00, 2014-01-12 15:00:00, 
  2014-01-12 16:00:00, 2014-01-12 17:00:00, 2014-01-12 18:00:00, 2014-01-12 19:00:00, 
  2014-01-12 20:00:00, 2014-01-12 21:00:00, 2014-01-12 22:00:00, 2014-01-12 23:00:00, 
  2014-01-13 00:00:00, 2014-01-13 01:00:00, 2014-01-13 02:00:00, 2014-01-13 03:00:00, ...], ['A', 'B']]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Pandas 同时为每组填充缺失的日期和值

将缺失值估算为0,并在Pandas中创建指标列

读取pandas dataFrame列中的日期作为基准日以生成每小时数据

当分钟和秒为0时,倒数计时器不会减少小时数

Pandas:获取恰好为 0 的行

Python Pandas - Scipy 的优势比(P 值 = 0?)

显示不以“ .0” Python Pandas结尾的值

删除列值<0的Pandas DataFrame行

KeyError:访问pandas系列中的值时为0

如何隐藏PANDAS和Jupyter的索引0

Python Pandas:根据小时和日期绘制值

将具有日期时间索引的日期/小时数据帧分解为单个列-python,pandas

PHP DateTime与时区的小时数差异为0而不是2

将pandas日期时间转换为从开始算起的小时数

索引错误:索引 0 超出了在 pandas/matlplotlib 中大小为 0 的轴 0 的范围

Group By - Python / Pandas 中按类别分类的总小时数和小时数

Pandas:将 0 添加到表示日期的长整数

Python Pandas:Groupby Cumulative Sum,但避免标志为 0 的 sum

Pandas DataFrames KeyError:0

Pandas 用固定日期填充缺失的日期值

在Pandas数据框中以最少的日期填充缺失的日期值

在 Python 中添加数量为 0 的缺失日期

只有值不为0时,Python / Pandas才会减去

在 Pandas DataFrame 中用 0 替换不是 INT 的值

在 Pandas DF 中用 1 (else = 0) rowwise 替换 MAX 值

在pandas中选择值大于0的所有列名

在Pandas DataFrame中将列从0更改为逐行更改值

在python中将pandas列值从0缩放到1

Python Pandas:获取先前值大于 0 的行