Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a column in a dataframe is an integer or not, and perform operation

Tags:

pandas

Check whether a column in a dataframe is an integer or not, and if it is an integer, it must be multiplied by 10

import numpy as np
import pandas as pd
df = pd.dataframe(....)    
#function to check and multiply if a column is integer
def xtimes(x): 
  for col in x:
    if type(x[col]) == np.int64:
        return x[col]*10
    else:
        return x[col]
#using apply to apply that function on df
df.apply(xtimes).head(10)

I am getting an error like ('GP', 'occurred at index school')

like image 466
yaswanthkumar gothireddy Avatar asked Mar 08 '23 15:03

yaswanthkumar gothireddy


1 Answers

You could use select_dtypes to get numeric columns and then multiply.

In [1284]: df[df.select_dtypes(include=['int', 'int64', np.number]).columns] *= 10

You could have your specific check list for include=[... np.int64, ..., etc]

like image 108
Zero Avatar answered Jun 10 '23 22:06

Zero