2

Is there an easy way to round df values but exclude a given row based on name?

This is how I am rounding the values in my df:

#multiply value by 100 then round
df = df.applymap(lambda x: int(round(x*100))) 

However, I want to exclude "Mean" from the rounding. The position/index of "Mean" is unknown, it is not fixed. Number of rows in the df are also unknown.

Below, I have shown how the df looks like "before", how it looks like "after" I apply my code and what I would like the df to look like "desired".

enter image description here

Boosted_d16
  • 13,340
  • 35
  • 98
  • 158

1 Answers1

1

You can do it this way:

df.loc[df.index != 'Mean'] = df.applymap(lambda x: int(round(x*100))) 

Would return

       male     female
honda   48.0    24.0
ford    78.0    48.0
nissan  69.0    12.0
jag     47.0    36.0
Mean    102.0   204.0 
BICube
  • 4,451
  • 1
  • 23
  • 44