Pandas, select maximum from one column and minimum from another

filippo

I have a dataframe like this

A         B
25        0.5
21        0.6
17        0.7
14        0.7   <--- this is the row I want
12        0.3

I'd like to select the maximum B with minimum A.

Is there a pandas easy trick to do this?

jezrael

First compare column B by max values and then get index of minimal A by idxmin, last select by loc:

a = df.loc[df['B'] == df['B'].max(), 'A'].idxmin()
print (a)
3

#for one row DataFrame use [[]]
df = df.loc[[a]]
print (df)
    A    B
3  14  0.7

#for Series use []
s = df.loc[a]
print (s)
A    14.0
B     0.7
Name: 3, dtype: float64

Detail:

print (df.loc[df['B'] == df['B'].max(), 'A'])

2    17
3    14
Name: A, dtype: int64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select maximum/minimum with another column

From Dev

How to select pandas row with maximum value in one column, from a group of rows that share two common columns?

From Dev

Select maximum value from one column by second column

From Dev

MariaDB: Select the fields from one column in one table that are not in a subset of another column from another table

From Dev

Subtract a column from one pandas dataframe from another

From Dev

Subtract a column from one pandas dataframe from another

From Dev

Pandas: select from column with index corresponding to values in another column

From Dev

R: return a value from one column in a data frame corresponding to the minimum value in another column

From Dev

how to extract maximum and minimum value from column 1 and column 2

From Dev

select multiple column from one table and insert into another as rows

From Dev

select multiple columns with max from one column and distinct on another

From Dev

Select from one table and order by column of another table

From Dev

while creating a table select one column from another table sql

From Dev

Adding column(s) from one dataframe to another python pandas

From Dev

Pandas copy column names from one dataframe to another

From Dev

Python Pandas - copy substring from one column to another

From Dev

Referencing a column in a SELECT from another column in that SELECT

From Dev

Maximum of element from List based on the another one

From Dev

Maximum of element from List based on the another one

From Dev

pandas get minimum of one column in group when groupby another

From Dev

Select a column from maximum cell to first cell

From Dev

Select all columns from one table and one from another where column equals variable

From Java

Derive pandas column from another

From Dev

Retrieve the minimum value of a column from the max value of another column

From Dev

Select from one table and count from another

From Dev

Select one entry from another Select

From Dev

Select from a column which is saved in another column

From Dev

Select status from a column to populate another column

From Dev

Copy data from one column into another column

Related Related

  1. 1

    Select maximum/minimum with another column

  2. 2

    How to select pandas row with maximum value in one column, from a group of rows that share two common columns?

  3. 3

    Select maximum value from one column by second column

  4. 4

    MariaDB: Select the fields from one column in one table that are not in a subset of another column from another table

  5. 5

    Subtract a column from one pandas dataframe from another

  6. 6

    Subtract a column from one pandas dataframe from another

  7. 7

    Pandas: select from column with index corresponding to values in another column

  8. 8

    R: return a value from one column in a data frame corresponding to the minimum value in another column

  9. 9

    how to extract maximum and minimum value from column 1 and column 2

  10. 10

    select multiple column from one table and insert into another as rows

  11. 11

    select multiple columns with max from one column and distinct on another

  12. 12

    Select from one table and order by column of another table

  13. 13

    while creating a table select one column from another table sql

  14. 14

    Adding column(s) from one dataframe to another python pandas

  15. 15

    Pandas copy column names from one dataframe to another

  16. 16

    Python Pandas - copy substring from one column to another

  17. 17

    Referencing a column in a SELECT from another column in that SELECT

  18. 18

    Maximum of element from List based on the another one

  19. 19

    Maximum of element from List based on the another one

  20. 20

    pandas get minimum of one column in group when groupby another

  21. 21

    Select a column from maximum cell to first cell

  22. 22

    Select all columns from one table and one from another where column equals variable

  23. 23

    Derive pandas column from another

  24. 24

    Retrieve the minimum value of a column from the max value of another column

  25. 25

    Select from one table and count from another

  26. 26

    Select one entry from another Select

  27. 27

    Select from a column which is saved in another column

  28. 28

    Select status from a column to populate another column

  29. 29

    Copy data from one column into another column

HotTag

Archive