我有一个包含多个观测值的数据框:
date colour orders
2014-10-20 red 7
2014-10-21 red 10
2014-10-20 yellow 3
我想重新索引数据框并标准化日期。
date colour orders
2014-10-20 red 7
2014-10-21 red 10
2014-10-22 red NaN
2014-10-20 yellow 3
2014-10-21 yellow NaN
2014-10-22 yellow NaN
我想按照 colour
和 date
对数据框进行排序,然后尝试重新索引。
index = pd.date_range('20/10/2014', '22/10/2014')
test_df = df.sort(['colour', 'date'], ascending=(True, True))
ts = test_df.reindex(index)
ts
但它返回一个具有正确索引但所有NaN
值的新数据帧。
date colour orders
2014-10-20 NaN NaN
2014-10-21 NaN NaN
2014-10-22 NaN NaN
index
是什么? - jorists = test_df.reindex(index)
这一行中,您使用了一个名为“index”的变量。那具体是什么呢? - joristest_df.resample('D')
是用来做这个的,但我认为我应该先按“日期”对test_df进行索引,这是我正在努力解决的问题。 - Gianluca