简体   繁体   中英

Select rows from a DataFrame using .loc and multiple conditions and then show the row corresponding to the min/max of one column

I know how to select data using .loc and multiple conditions, like so:

df.loc[(df['A'] == True)&(df['B'] == 'Tuesday')] 

But from the result of this I can't figure out how to show the entire row corresponding to the min (or max) taken on one other column of numbers, 'C'. How do I do this?

Use this:

df2 = df.loc[(df['A'] == True)&(df['B'] == 'Tuesday')]

df2.loc[df2.C == df2.C.min(), :]

You can use the idxmin or idxmax functions.

Docs for the idxmin function : "Return the row label of the minimum value. If multiple values equal the minimum, the first row label with that value is returned."

So, if you df.loc[((df['A'] == True) & (df['B'] == 'Tuesday')).idxmix()] , this will return the row which has the minimum value for column C.

Use this:

for columns:

df.loc[(df['A'] == True)&(df['B'] == 'Tuesday')].apply(max, axis=0) 

for rows:

df.loc[(df['A'] == True)&(df['B'] == 'Tuesday')].apply(max, axis=1) 

The easiest option:

df = pd.DataFrame({
          'A': [True,False,True,True],
          'B': ['Sun', 'Mon', 'Tue', 'Tue'],
          'C': [1,4,5,1],
          'D': [10,20,30,40]})

print(df.query(""" A == True and B == 'Tue' and C == C.min() """))

      A    B  C   D
3  True  Tue  1  40

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM