Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operation on pandas dataframe depending on type

Is there any elegant way in a Pandas Dataframe with several columns of float64 and other types to make a global operation only on the float64 elements?

What I am looking for is to divide all float64 elements by 100, without having to target or name specific columns.

like image 402
airzed Avatar asked Dec 07 '25 05:12

airzed


1 Answers

You could use df.select_dtypes().

cols = df.select_dtypes(include=['float64']).columns
df[cols] = df[cols] / 100.

This will divide all float64 columns by 100. Note that if you have mixed object columns with some float64 cells, they won't be divided (your question wasn't totally clear about whether you wanted to act on columns or elements)

like image 69
ASGM Avatar answered Dec 08 '25 17:12

ASGM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!