Reputation: 377
if I have a DataFrame consisting of some columns that only consist of strings, and other columns that only consist of numeric types (i.e. float or int), how to I multiply each float or int element in the DataFrame by some constant?
For example, if the constant is 3, and I have this data frame:
import numpy as np
import pandas as pd
np.random.seed(seed=9876)
df1 = pd.DataFrame(['a']*3+['b'])
df2 = pd.DataFrame(['x','y','z', 't'])
df3 = pd.DataFrame(np.round(np.random.randn(4,2),2)*10)
df = pd.concat([df1, df2, df3], axis = 1)
df.columns = ['ind', 'x1', 'x2','x3']
df = df.set_index('ind')
print(df)
x1 x2 x3
ind
a x 3.9 -10.9
a y 2.1 3.2
a z -9.3 0.3
b t -11.1 -1.2
The output should look like:
x1 x2 x3
ind
a x 11.7 -32.7
a y 6.3 9.6
a z -27.9 0.9
b t -33.3 -3.6
One idea is to use pd.concat([df['x1'], df.loc[:,['x2', 'x3']].mul(3)], axis = 1)
, but I won't necessarily know ex-ante which are the string vs numeric columns when applying this to numerous data frames. It also seems inefficient. I'm sure a better method exists but I'm yet to come up with one.
Upvotes: 0
Views: 1229
Reputation: 862751
Select only columns with numeric by DataFrame.select_dtypes
and then multiple only them:
cols = df.select_dtypes(np.number).columns
print (cols)
Index(['x2', 'x3'], dtype='object')
df[cols] = df[cols].mul(3)
print (df)
x1 x2 x3
ind
a x 11.7 -32.7
a y 6.3 9.6
a z -27.9 0.9
b t -33.3 -3.6
Upvotes: 2