我有一个简单的问题,最好用一个例子来描述。我正在尝试基于 current_df
和 parent_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 应该:
- 包含
parent_df
的索引 - 如果索引不在
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/