Pandas, multiply all the numerical values ​​in the data frame by a constant

How to multiply all numeric values ​​in a data frame by a constant without having to explicitly specify column names? Example:

In [13]: df = pd.DataFrame({'col1': ['A','B','C'], 'col2':[1,2,3], 'col3': [30, 10,20]}) In [14]: df Out[14]: col1 col2 col3 0 A 1 30 1 B 2 10 2 C 3 20 

I tried df.multiply , but it also affects string values, concatenating them several times.

 In [15]: df.multiply(3) Out[15]: col1 col2 col3 0 AAA 3 90 1 BBB 6 30 2 CCC 9 60 

Is there a way to keep string values ​​constant while multiplying only numeric values ​​by a constant?

+5
source share
4 answers

you can use select_dtypes () , including number dtype or excluding all columns object and datetime64 dtypes:

Demo:

 In [162]: df Out[162]: col1 col2 col3 date 0 A 1 30 2016-01-01 1 B 2 10 2016-01-02 2 C 3 20 2016-01-03 In [163]: df.dtypes Out[163]: col1 object col2 int64 col3 int64 date datetime64[ns] dtype: object In [164]: df.select_dtypes(exclude=['object', 'datetime']) * 3 Out[164]: col2 col3 0 3 90 1 6 30 2 9 60 

or a much better solution (c) ayhan :

 df[df.select_dtypes(include=['number']).columns] *= 3 

From docs :

To select all numeric types, use numpy dtype numpy.number

+6
source

One way is to get dtypes , map them to object and datetime dtypes and exclude them using a mask, for example:

 df.ix[:,~np.in1d(df.dtypes,['object','datetime'])] *= 3 

Run Example -

 In [273]: df Out[273]: col1 col2 col3 0 A 1 30 1 B 2 10 2 C 3 20 In [274]: df.ix[:,~np.in1d(df.dtypes,['object','datetime'])] *= 3 In [275]: df Out[275]: col1 col2 col3 0 A 3 90 1 B 6 30 2 C 9 60 
+4
source

Another answer indicates how to multiply only numeric columns. Here's how to update it:

 df = pd.DataFrame({'col1': ['A','B','C'], 'col2':[1,2,3], 'col3': [30, 10,20]}) s = df.select_dtypes(include=[np.number])*3 df[s.columns] = s print (df) col1 col2 col3 0 A 3 90 1 B 6 30 2 C 9 60 
+2
source

This should work even on mixed types inside columns, but most likely slower on large data frames.

 def mul(x, y): try: return pd.to_numeric(x) * y except: return x df.applymap(lambda x: mul(x, 3)) 
+1
source

All Articles