![](/img/trans.png)
[英]Given a list of lists, how to get a group of lists like output_1 with python without pandas?
[英]Python/Pandas: How to Get the Last 3 of Each Group and Put into a List of Lists
我想通过“ AutoNumber”对这个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
使用以下代码...
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.