Используйте GroupBy.cumcount
с ascending=False
для счетчика и Series.reindex
с MultiIndex
, созданным MultiIndex.from_product
:
print (df)
id date temperature
0 1 2011-9-12 12
1 1 2011-9-12 20
2 1 2011-9-18 12
3 1 2011-9-19 90
4 2 2011-9-12 15
5 3 2011-9-12 15
6 3 2011-9-16 15
N = 3
df['new'] = df.groupby('id').cumcount(ascending=False)
mux = pd.MultiIndex.from_product([df['id'].unique(), range(N-1, -1, -1)], names=['id','new'])
df1 = (df.set_index(['id', 'new'])['temperature']
.reindex(mux, fill_value=0)
.reset_index(level=1, drop=True)
.reset_index())
print (df1)
id temperature
0 1 20
1 1 12
2 1 90
3 2 0
4 2 0
5 2 15
6 3 0
7 3 15
8 3 15
EDIT:
Если MultiIndex DataFrame
:
print (df)
temperature
id date
1 2011-9-12 12
2011-9-12 20
2011-9-18 12
2011-9-19 90
2 2011-9-12 15
3 2011-9-12 15
2011-9-16 15
print (df.index)
MultiIndex(levels=[[1, 2, 3], ['2011-9-12', '2011-9-16', '2011-9-18', '2011-9-19']],
codes=[[0, 0, 0, 0, 1, 2, 2], [0, 0, 2, 3, 0, 0, 1]],
names=['id', 'date'])
N = 3
df['new'] = df.groupby('id').cumcount(ascending=False)
mux = pd.MultiIndex.from_product([df.index.levels[0], range(N-1, -1, -1)], names=['id','new'])
df1 = (df.reset_index(level=1, drop=True)
.set_index('new', append=True)['temperature']
.reindex(mux, fill_value=0)
.reset_index(level=1, drop=True)
.reset_index())
print (df1)
id temperature
0 1 20
1 1 12
2 1 90
3 2 0
4 2 0
5 2 15
6 3 0
7 3 15
8 3 15