Evridiki
Evridiki

Reputation: 323

How to groupby records based on two parameters using pandas in python

If I need to group by my records on their minimum value I do the following

df.sort_values(['price']).drop_duplicates(['SKU']).to_csv('Min_Prices.csv',sep=';',index=False)

and I get

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

But I have an additional column of availability like

    id  price  availability   editor
 1  k1  8.0    0              ed2
 2  k1  9.0    1              ed1
 3  k1  9.5    1              ed3
 4  k4  9.5    1              ed1
 5  k4  9.0    0              ed2
 6  k5  8.5    1              ed1
 7  k5  7.8    0              ed2

The expected should be

k1 9.0 ed1
k4 9.5 ed1
k5 8.5 ed1

So how can I get minimums of availability of value 1. Where can I insert the availability in the initial formula?

thank you

Upvotes: 1

Views: 40

Answers (1)

jezrael
jezrael

Reputation: 863256

I believe you need boolean indexing or DataFrame.query:

df1 = df[df['availability'] == 1].sort_values(['price']).drop_duplicates(['id'])
#alternative
#df1 = df.query('availability == 1').sort_values(['price']).drop_duplicates(['id'])

print (df1)
   id  price  availability editor
6  k5    8.5             1    ed1
2  k1    9.0             1    ed1
4  k4    9.5             1    ed1

df1.to_csv('Min_Prices.csv',sep=';',index=False)

Upvotes: 1

Related Questions