Is there a command in Python Pandas to find the name of the maximum column after sorting?

Asked 2 years ago, Updated 2 years ago, 42 views

Is there a command in Pandas to find the name of the maximum column after sorting? I can't find it, so I'm at a loss.

index 0 March score

index April score

Please tell me how to find a subject whose grades have improved a lot between March and April

Subject (column name)

pandas

2022-09-21 16:15

1 Answers

You want to know the name of the maximum value, right? (The questions in the text are ambiguous...)

https://notebooks.azure.com/wincommerce/projects/hashcode/html/9226.ipynb

In [1]:
import pandas as pd

In [2]:
scores = pd.np.random.randint(100, size=(5, 4))
df = pd.DataFrame (scores, columns = ['Korean', 'English', 'Mathematics', 'Science'])

In [3]:
df

Out[3]:
Korean, English, Mathematics and Science
0   16  64  90  28
1   31  31  40  20
2   36  36  30  19
3   2   95  86  37
4   95  92  74  16
The largest value is 95, which is Korean and English¶

In [4]:
# Where are the biggest values of all subjects? Let's check the True value.
df == df.values.max()

Out[4]:
Korean, English, Mathematics and Science
0   False   False   False   False
1   False   False   False   False
2   False   False   False   False
3   False   True    False   False
4   True    False   False   False

In [7]:
# Let's get the coordinates of the largest score
ids, cols = pd.np.where(df == df.values.max())

In [8]:
# There are idedx and columns, respectively, so I made it into a tutelage to check.
list(zip(ids, cols))

Out[8]:
[(3, 1), (4, 0)]
It points to 95 rows: 3 columns:1 and 4 columns:0.¶

In [9]:
# Let's extract the column name
[df.columns[c] for c in cols]

Out[9]:
[English, Korean]


2022-09-21 16:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.