Home > database >  Change several columns of a dataframe to integer type. Error cannot convert the series to <class
Change several columns of a dataframe to integer type. Error cannot convert the series to <class

Time:05-27

Let it be the following Dataframe of pandas in Python:

code visit_1 visit_2 visit_3 S flag
0.0 2.0 1.0 2.0 True
0.0 0.0 0.0 0.0 False
1.0 0.0 1.0 1.0 False
2.0 3.0 1.0 0.0 True
3.0 2.0 3.0 1.0 False

I want to convert the first 4 columns of the above DataFrame to integer type:

cols = (df.filter(like='visit_').columns).append(df.filter(like='code').columns)
print(cols)

In the original DataFrame there are more columns, so I decided to create a filter by name of the columns I want to modify. I print on the screen what I get.

Index(['visit_1', 'visit_2',
       'visit_3 S', 'code'],
      dtype='object')

When I apply the type change to int, I get the following error:

df[cols] = df[cols].apply(lambda a: int(a), axis=1)

TypeError: cannot convert the series to <class 'int'>

My idea is to obtain the following result:

code visit_1 visit_2 visit_3 flag
0 2 1 2 True
0 0 0 0 False
1 0 1 1 False
2 3 1 0 True
3 2 3 1 False

I am grateful for any help you can offer me.

CodePudding user response:

You should change this line of code to achieve the desired result.
df[cols] = df[cols].apply(lambda a:a.astype(int),axis=1)

  • Related