I would like to group this DataFrame by "AutoNumber" and get the last 2 Amounts of each, then put into a list of lists
我想通过“自动编号”对此DataFrame进行分组并获取每个的最后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
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
Using the following code...
使用以下代码......
Amount_ref = group.groupby('AutoNumber')['Amount'].tail(2)
I got the output...
我得到了输出......
0 1667.35
1 4181.28
4 147.51
5 147.51
Name: Amount, dtype: float64
But my desired output is...
但我想要的输出是......
[[1667.35, 4181.28], [147.51, 147.51]]
1 个解决方案
#1
1
You can use apply
and tolist
:
您可以使用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]]
Or:
要么:
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]]
#1
1
You can use apply
and tolist
:
您可以使用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]]
Or:
要么:
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]]