Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get groupby records based on their minimum value using pandas in python

I have the following 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

If I do the following

import pandas as pd 

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

Instead of getting

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

I get

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

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

like image 481
Evridiki Avatar asked Apr 23 '26 02:04

Evridiki


1 Answers

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 
like image 130
Vaishali Avatar answered Apr 25 '26 17:04

Vaishali