Python Pandas DataFrame组通过选择列 [英] Python pandas dataframe groupby selecting columns
本文介绍了Python Pandas DataFrame组通过选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下detaframe bb:
I have the following detaframe bb:
bq_selection_id bq_balance bq_market_id bq_back_price
0 45094462 185.04 7278437 1.97
1 45094462 185.04 7278437 1.97
2 45094463 185.04 7278437 3.05
3 45094463 185.04 7278437 3.05
4 45094464 185.04 7278437 5.80
5 45094464 185.04 7278437 5.80
6 45094466 185.04 7278437 200.00
7 45094466 185.04 7278437 200.00
8 45094465 185.04 7278437 NaN
9 45094465 185.04 7278437 NaN
我想按"market_id"分组并采用前两个最低的"bq_back_price".我设法用
I would like to group by "market_id" and take first two lowest "bq_back_price". I managed to do this with
bb.groupby('bq_market_id')['bq_back_price'].nsmallest(2)
问题是我缺少某些列,例如"bq_selection_id","bq_balance"和"bq_back_price"列没有名称.那就是我得到的
The problem is that I am missing some of the columns such as "bq_selection_id", "bq_balance" and column "bq_back_price" does not have name. That is what I get
bq_market_id
7278437 0 1.97
7278437 1 1.97
我想得到这样的东西
bq_selection_id bq_balance bq_market_id bq_back_price
0 45094462 185.04 7278437 1.97
1 45094462 185.04 7278437 1.97
你能帮我吗?
推荐答案
您可以先在bq_back_price
上对值进行排序,然后在每个组中使用head(2)
.
You can first sort values on bq_back_price
, and then take head(2)
in each groups.
In [218]: df.sort_values('bq_back_price').groupby('bq_market_id').head(2)
Out[218]:
bq_selection_id bq_balance bq_market_id bq_back_price
0 45094462 185.04 7278437 1.97
1 45094462 185.04 7278437 1.97
这篇关于Python Pandas DataFrame组通过选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文