无法使用python中的pandas根据它们的最小值获取groupby记录 [英] Cannot get groupby records based on their minimum value using pandas in python

查看:56
本文介绍了无法使用python中的pandas根据它们的最小值获取groupby记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下csv

id;price;editor
k1;10,00;ed1
k1;8,00;ed2
k3;10,00;ed1
k3;11,00;ed2
k2;10,50;ed1
k1;9,50;ed3

如果我执行以下操作

import pandas as pd 

df = pd.read_csv('Testing.csv', delimiter =';')
df_reduced= df.groupby(['id', 'editor'])['price'].min()

而不是

k1;8,00;ed2
k2;10,50;ed1
k3;10,00;ed1

我知道

k1;10,00;ed1
    8,00;ed2
    9,50;ed3
k2;10,50;ed1
k3;10,00;ed1
   11,00;ed2 

那么我可以得到三个ID的最小值吗?

So can I get three id's with their minimum values?

推荐答案

仅按ID对数据进行分组,然后找到每个组的最低价格.根据最小值索引原始数据框,以包括编辑器列.

Group the data by only id and find min price for each group. Index the original dataframe based on the minimum values to include the editor column.

注意:我假设价格栏中的逗号是错字

Note: I am assuming that the comma in price column is a typo

df.loc[df['price'] == df.groupby('id')['price'].transform('min')]


    id  price   editor
1   k1  8.0     ed2 
2   k3  10.0    ed1 
4   k2  10.5    ed1 

这篇关于无法使用python中的pandas根据它们的最小值获取groupby记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆