I would like to select all rows that have Points==0 and have the minimum value in the Day column from my dataset.
Dataframe
Points Day Name
55 0 Jon
0 7 Ron
0 8 Sam
44 6 Chris
0 7 Joan
49 2 Greg
What I'm hoping to get
Points Day Name
0 7 Ron
0 7 Joan
I have tired this code but I only get the first instance where this is true.
df1 = df.loc[[df.loc[df.points == 0, 'Day'].idxmin()]]
How do I get all rows?
You can use the min function to get the minimum value from the filtered dataset where Points == 0 and then use it to filter the whole dataset.
df[(df["Points"] == 0) & (df["Day"] == min(df[df["Points"] == 0]["Day"]))]
Now it works:
>>> df
Points Day
0 55 0
1 0 7
2 0 8
3 44 6
4 0 7
5 49 2
>>> df[(df["Points"] == 0) & (df["Day"] == min(df[df["Points"] == 0]["Day"]))]
Points Day
1 0 7
4 0 7
IIUC
df.query('Points==0').loc[lambda x : x['Day']==x['Day'].min()]
Out[207]:
Points Day Name
1 0 7 Ron
4 0 7 Joan
You can do it the following way:
df[(df['Points']==0) & (df['Day']==df[df['Points']==0]['Day'].min())]
The & means AND in pandas boolean indexing, if you want to use OR you can use |.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.