简体   繁体   English

我如何 select 满足另一列 pandas 条件的最小值的所有行

[英]How do I select all rows with a minimum value that meet a condition in another column pandas

I would like to select all rows that have Points==0 and have the minimum value in the Day column from my dataset.我想 select 所有具有 Points==0 并且在我的数据集中的 Day 列中具有最小值的行。

Dataframe 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.您可以使用 min function 从 Points == 0 的过滤数据集中获取最小值,然后使用它来过滤整个数据集。

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 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 |. & 表示 pandas boolean 索引中的 AND,如果要使用 OR,可以使用 |。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫:如何选择符合条件的数据框的所有行(ValueError:“数组长度不同”) - pandas: how to select an all rows of a dataframe that meet a condition (ValueError: “Arrays were different lengths”) 给定来自另一列的条件,如何遍历特定 Pandas DataFrame 列的行? - How do I iterate over the rows of a specific Pandas DataFrame column, given a condition from another column? Pandas - 如何根据唯一列值删除行,其中另一个列值是最小值并处理空值? - Pandas - How to drop rows based on a unique column value where another column value is a minimum and handling nulls? Pandas GroupBy 和 select 行在特定列中具有最小值 - Pandas GroupBy and select rows with the minimum value in a specific column 过滤分组的熊猫数据框,保留列中具有最小值的所有行 - Filter grouped pandas dataframe, keep all rows with minimum value in column 如果某些条件满足熊猫,如何翻转列 - How to flip column if certain condition is meet pandas 如何在熊猫中找到满足条件的所有行 - How to find all rows that meet a condition in Panda Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 对于另一列中的特定值,我如何 select pandas 中的列中的值计数? - How do I select the counts of values in a column in pandas for a specific value in another column? 如何根据数据框的另一列中的条件查找列中的最小值? - How to find minimum value in a column based on condition in an another column of a dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM