Home > database >  Find which column has the minimum value of a sum of all rows, and having the the name of the column
Find which column has the minimum value of a sum of all rows, and having the the name of the column

Time:11-17

I have a sum of all rows of columns y1 to y7 of a data frame

y1     4.475017e 02
y2     4.825798e 02
y3     4.077346e 04
y4     1.083712e 04
y5     4.005989e 04
y6     4.223634e 02
y7     3.385693e 01

I need to find which column has min value, in this case it is y7, so I want the output to be just: y7

What I did:

minimum = sum.min()

output: 33.85692709115603

ideal = sum.loc[sum == minimum]

output:

y7    33.856927
dtype: float64

I had to print and manually insert df["y7"] later.

I want to be able to do this without printing, that is, inserting df["y7"] but without having to actually write y7 since this will be the output of the prior input.

Edit:

Now that I have the output I wanted: min = y7, how do I mention a column with same name as the output?

Example: I need the output of the input df["y7"], but instead of writing y7, I want to write the output of min.

CodePudding user response:

You can use:

df.sum().idxmin()

Example:

print(df)

    A   B   C   D
0   0   1   2   3
1   4   5   6   7
2   8   9  10  11
3  12  13  14  15

df.sum().idxmin()

'A'

referencing the column:

col = df.sum().idxmin()

df[col]   # or without variable df[df.sum().idxmin()]

0     0
1     4
2     8
3    12
Name: A, dtype: int64
  • Related