in a dataframe using Pandas, I'd like to know how to find a column name which has the minimum or maximum value for a given row, not all the rows. I've been searching for answers and i kept reaching the following answer : df.idxmin(axis=1) This gives all the minimum values for each rows, but I am only seeking the comumn for one specific row. Thanks for your help !
CodePudding user response:
The line
df.max().argmax()
provides the column index in which the largest element resides (or the first column index of the largest elements, when the column index is non-unique).
In turn,
df.iloc[i].argmax()
provides the index for the row with index i
.
CodePudding user response:
import pandas as pd
data = {'store_1': [10, 20, 40],
'store_2': [50, 70,80],
'store_3': [5000, 20,10]}
df = pd.DataFrame.from_dict(data)
My FOO data for example::
store_1 | store_2 | store_3 | |
---|---|---|---|
0 | 10 | 50 | 5000 |
1 | 20 | 70 | 20 |
2 | 40 | 80 | 10 |
To find on dataframe the column name with min
value
df.iloc[0].idxmin()
'store_1'
To find on dataframe the columns with max
value
df.iloc[0].idxmax()
'store_3'
Find the min
value from store_1
col:
df.store_1.min()
10
Find the max
value from store_1
col:
df.store_1.max()
40
Find the min
value from store_2
col:
df.store_2.min()
50
Find the max
value from store_2
col:
df.store_2.max()
80
Find the min
value from store_3
col:
df.store_3.min()
10
Find the max
value from store_3
col:
df.store_3.max()
5000
Just remember that the syntax df.store_1
is the same of df['store_1']
...
Hope that helps