Skip to content
Advertisement

How to identify minimum squared value of an entire pandas dataframe column by column?

I have a pandas dataframe like this:

JavaScript

How could I calculate the sum of the squared values for the entire column (I am trying something like deviation = df[columnName].pow(2).sum() in a loop, but ideas are very welcome!) but also afterwards identifying the column that has the smallest of those sums and the actual smallest sum?

Edit: Adding desired output

Desired output in this case would be:

JavaScript

Advertisement

Answer

You can calculate the sum of square on the entire data frame, which returns a Series object with the column names as index. And then you can find the minimum value as well as minimum index using min and idxmin:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement