我有一个这样的数据框:
pd.DataFrame({'group': {0: 1, 1: 1, 2: 1, 3: 1, 4: 2, 5: 2, 6: 2}, 'year': {0: 2007, 1: 2008, 2: 2009, 3: 2010, 4: 2006, 5: 2007, 6: 2008}, 'amount': {0: 2.0, 1: 4.0, 2: np.nan, 3: 7.0, 4: 8.0, 5: 10.0, 6: 12.0}}])
group year amount
0 1 2007 2
1 1 2008 4
2 1 2009 NaN
3 1 2010 7
4 2 2006 8
5 2 2007 10
6 2 2008 12
我想为每个组添加最小值(删除NA),最大值(删除NA),滞后1和滞后2。我理想的数据框看起来像这样
group year amount min max lag1 lag2
0 1 2007 2 2 7 NaN NaN
1 1 2008 4 2 7 2 NaN
2 1 2009 NaN 2 7 4 2
3 1 2010 7 2 7 NaN 4
4 2 2006 8 8 12 NaN NaN
5 2 2007 10 8 12 8 NaN
6 2 2008 12 8 12 10 8
我知道要在熊猫中使用agg
进行最小值和最大值,但是我不确定如何按组处理滞后现象
答案 0 :(得分:3)
IIUC
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<form>
<input type="number" min="100" max="999999" step="1" stepcustom="100" />
<input type="submit" />
</form>