将数据框的几列更改为整数类型。错误无法将系列转换为 <class 'int'>

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

设为Python中pandas的如下Dataframe:

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

我想把上面DataFrame的前4列转成整型:

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

在原来的DataFrame中有更多的列,所以我决定按要修改的列的名称创建一个过滤器。我在屏幕上打印我得到的。

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

当我将类型更改应用于 int 时,出现以下错误:

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

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

我的想法是得到如下结果:

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

非常感谢您能为我提供的任何帮助。

您应该更改这行代码以获得所需的结果。
df[cols] = df[cols].apply(lambda a:a.astype(int),axis=1)