Skip to content
Advertisement

Pandas Average of row ignoring 0

I have a DataFrame that looks like this:

JavaScript

I need to find the mean of each row, ignoring instances of 0. My initial plan was to replace 0 with NaN and then get the mean excluding NaN.

I tried to replace 0 with NaN, however this didn’t work, and the DataFrame still contained 0. I tried:

JavaScript

The second issue is when I tried to calculate the mean, even knowing 0 would be included, the mean could not be calculated. I used:

JavaScript

The outcome was:

JavaScript

How can I accomplish this?

Advertisement

Answer

I tried to replace 0 with NaN, however this didn’t work, and the DataFrame still contained 0. I tried:

Convert your string values to numeric

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