Pandas, Multiply All The Numeric Values In The Data Frame By A Constant
How to multiply all the numeric values in the data frame by a constant without having to specify column names explicitly? Example: In [13]: df = pd.DataFrame({'col1': ['A','B','C']
Solution 1:
you can use select_dtypes() including number
dtype or excluding all columns of object
and datetime64
dtypes:
Demo:
In[162]: dfOut[162]:
col1col2col3date0A1302016-01-011B2102016-01-022C3202016-01-03In[163]: df.dtypesOut[163]:
col1objectcol2int64col3int64datedatetime64[ns]dtype: objectIn[164]: df.select_dtypes(exclude=['object', 'datetime']) * 3Out[164]:
col2col3039016302960
or a much better solution (c) ayhan:
df[df.select_dtypes(include=['number']).columns] *= 3
From docs:
To select all numeric types use the numpy dtype numpy.number
Solution 2:
The other answer specifies 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
Solution 3:
One way would be to get the dtypes
, match them against object
and datetime
dtypes and exclude them with a mask, like so -
df.ix[:,~np.in1d(df.dtypes,['object','datetime'])] *= 3
Sample run -
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
Solution 4:
This should work even over mixed types within columns but is likely slow over large dataframes.
defmul(x, y):
try:
return pd.to_numeric(x) * y
except:
return x
df.applymap(lambda x: mul(x, 3))
Post a Comment for "Pandas, Multiply All The Numeric Values In The Data Frame By A Constant"