Python/Pandas:如何获取每个组的最后3个并将其放入列表中
我想通过“自动编号”对这个数据帧进行分组,并获得每个数据帧的最后2个数量,然后将其放入列表中Python/Pandas:如何获取每个组的最后3个并将其放入列表中,python,pandas,indexing,dataframe,group-by,Python,Pandas,Indexing,Dataframe,Group By,我想通过“自动编号”对这个数据帧进行分组,并获得每个数据帧的最后2个数量,然后将其放入列表中 LoanAgreementID Amount TransactionDate \ 0 252357C2-24C2-E611-8126-06CAB7997043 1667.35 2016-12-14 1 252357C2-24C2-E611-8126-06CAB7997043 4181.28 2016-12-14
LoanAgreementID Amount TransactionDate \
0 252357C2-24C2-E611-8126-06CAB7997043 1667.35 2016-12-14
1 252357C2-24C2-E611-8126-06CAB7997043 4181.28 2016-12-14
2 4BF6F3D3-30C2-E611-8126-06CAB7997043 1667.35 2016-12-14
3 4BF6F3D3-30C2-E611-8126-06CAB7997043 4181.28 2016-12-14
4 4BF6F3D3-30C2-E611-8126-06CAB7997043 147.51 2017-01-18
5 4BF6F3D3-30C2-E611-8126-06CAB7997043 147.51 2017-02-01
ContactID PaymentType CashLedgerType \
0 000FF848-42BE-E611-8126-06CAB7997043 NaN 5
1 000FF848-42BE-E611-8126-06CAB7997043 NaN 5
2 000FF848-42BE-E611-8126-06CAB7997043 NaN 5
3 000FF848-42BE-E611-8126-06CAB7997043 NaN 5
4 000FF848-42BE-E611-8126-06CAB7997043 0.0 3
5 000FF848-42BE-E611-8126-06CAB7997043 0.0 3
KeyValue_String KeyValue_String.1 AutoNumber IssueDate date_helper
0 Cheque NaN 54940 2016-12-14 2016-12-14
1 Cheque NaN 54940 2016-12-14 2016-12-14
2 Cheque NaN 54945 2016-12-14 2016-12-14
3 Cheque NaN 54945 2016-12-14 2016-12-14
4 Payment PAP 54945 2016-12-14 2017-01-18
5 Payment PAP 54945 2016-12-14 2017-02-01
0 1667.35
1 4181.28
3 4181.28
4 147.51
5 147.51
Name: Amount, dtype: float64
使用以下代码
Amount_ref = group.groupby('AutoNumber')['Amount'].tail(2)
我得到了输出
0 1667.35
1 4181.28
4 147.51
5 147.51
Name: Amount, dtype: float64
但是我想要的结果是
[[1667.35, 4181.28], [147.51, 147.51]]
您可以使用
apply
和tolist
:
Amount_ref = group.groupby('AutoNumber')['Amount']
.apply(lambda x: x.tail(2).tolist()).tolist()
print (Amount_ref)
[[1667.35, 4181.28], [147.51, 147.51]]
或:
Amount_ref = group.groupby('AutoNumber')['Amount']
.apply(lambda x: x.iloc[-2:].tolist()).tolist()
print (Amount_ref)
[[1667.35, 4181.28], [147.51, 147.51]]