Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'], '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 affects the string values as well by 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 preserve the string values intact while multiplying only the numeric values by a constant?

like image 357
CentAu Avatar asked Jul 23 '16 15:07

CentAu


People also ask

How do you multiply a constant in a data frame?

Use the * operator to multiply a column by a constant number Select a column of DataFrame df using syntax df["column_name"] and set it equal to n * df["column_name"] where n is the number to multiply by.

How do you multiply numbers in a pandas DataFrame?

The mul() method multiplies each value in the DataFrame with a specified value. The specified value must be an object that can be multiplied with the values of the DataFrame.

How do you multiply values in pandas Series?

multiply() function perform the multiplication of series and other, element-wise. The operation is equivalent to series * other , but with support to substitute a fill_value for missing data in one of the inputs.

How do you multiply all rows in a DataFrame?

First of all, create a data frame with multiple rows and a data frame with single row. Then, use mapply function to multiply row values in the data frame having multiple rows with single row data frame.


2 Answers

you can use select_dtypes() including number dtype or excluding all columns of 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 the numpy dtype numpy.number

like image 50
MaxU - stop WAR against UA Avatar answered Sep 20 '22 09:09

MaxU - stop WAR against UA


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
like image 21
Jossie Calderon Avatar answered Sep 20 '22 09:09

Jossie Calderon