python - 使用特定日期时间索引重新索引 Pandas Dataframe

标签 python pandas datetime reindex

我有一个简单的问题,最好用一个例子来描述。我正在尝试基于 current_dfparent_df 渲染 output_df

current_df:带有日期时间索引的简单时间序列 df

Index                            Val
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43

parent_df:另一个简单的时间序列df

Index                            Val
'10-1-2010 07:00:00'              0.23
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43
'10-1-2010 11:00:00'              2.23

output_df 应该:

  1. 包含 parent_df 的索引
  2. 如果索引不在 current_df 中,则包含 0 值
Index                             Val
'10-1-2010 07:00:00'              0
'10-1-2010 08:00:00'              1.23
'10-1-2010 09:00:00'              1.3
'10-1-2010 10:00:00'               1.43
'10-1-2010 11:00:00'                0

这应该是一项简单的任务 - 我只是一片空白。

干杯。

最佳答案

我认为这是reindex的功能

output_df = current_df.reindex(parent_df.index, fill_value=0)

关于python - 使用特定日期时间索引重新索引 Pandas Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405706/

相关文章:

python - 纠正不完整的年份 - Python

c# - 如何检查例如 30 分钟是否已经过去?

php - 从 int 字段中减去 php mysql 查询中的时间

python - 在 Google App Engine Python 运行时中解析从字符串到日期时间的 UTC 偏移量

python - 如何更改python时间序列中的重采样顺序

python - 为什么 dict(dict) 得到相同的字典?

python - 如何在不手动编写的情况下将障碍物分配到我的网格?

python - 更改浮点格式以同时包含小数和逗号

python - 减去两个索引不重叠的 DataFrame

python - Pandas 在 DataFrame 中插入行小计