Home > Blockchain >  Divide entire dataframe by 2 using Python
Divide entire dataframe by 2 using Python

Time:11-16

I wish to divide my entire dataframe by 2 in Python

Data

id      date    aa  bb  cc  dd
staff   Q1 23   6   2   6   0
staff2  Q2 23   6   2   6   0

Desired

id      date    aa  bb  cc  dd
staff   Q1 23   3   1   3   0
staff2  Q2 23   3   1   3   0

Doing

df.div(2)

Any suggestion is appreciated

Receiving TypeError - update

Wish to divide numerical columns by 2

CodePudding user response:

First we need select the number

df.update(df.select_dtypes(np.number)/2)

CodePudding user response:

check for float 64 and int 64 data type for a column name then use the column to divide by 2 the results

for col in df.select_dtypes(include=['float64', 'int64']).columns:
        df[col] = df[col] / 2
  • Related