Python 数据帧中过去可能缺少的月份的值
假设我有一个如下所示的数据帧:Python 数据帧中过去可能缺少的月份的值,python,pandas,time-series,Python,Pandas,Time Series,假设我有一个如下所示的数据帧: Month, Gender, State, Value 2010-01, M, S1, 10 2010-02, M, S1, 20 2010-05, M, S1, 26 2010-03, F, S2, 11 我想为上个月(或X过去几个月)的给定性别和州添加另一列(如果存在),即: Month, Gender, State, Value, Last Value 2010-01, M,
Month, Gender, State, Value
2010-01, M, S1, 10
2010-02, M, S1, 20
2010-05, M, S1, 26
2010-03, F, S2, 11
我想为上个月(或X
过去几个月)的给定性别和州添加另一列(如果存在),即:
Month, Gender, State, Value, Last Value
2010-01, M, S1, 10, NaN
2010-02, M, S1, 20, 10
2010-05, M, S1, 26, NaN (there is no 2010-04 for M, S1)
2010-03, F, S2, 11, NaN
我知道我必须
groupby(['Gender','State'])
,但是shift()
不起作用,因为它只是按行数移动数据,它不知道周期本身(如果缺少一个月)。我找到了一种方法,对此不太满意:
full_index = []
for g in all_genders:
for s in all_states:
for m in all_months:
full_index.append((g, s, m))
df = df.set_index(['Gender', 'State', 'Month'])
df = df.reindex(full_index) # fill in all missing values
因此,基本上,我们不需要处理数据中缺少的行,只需创建缺少的行,shift()
即:
我找到了这样做的方法,对此不太满意:
full_index = []
for g in all_genders:
for s in all_states:
for m in all_months:
full_index.append((g, s, m))
df = df.set_index(['Gender', 'State', 'Month'])
df = df.reindex(full_index) # fill in all missing values
因此,基本上,我们不需要处理数据中缺少的行,只需创建缺少的行,shift()
即: