To apply a function over multiple columns in a DataFrame, the list of columns can be iterated over using a for loop. In the following example, a predefined list of columns is converted from the string type to the numeric type:
df['new_col5'] = ['7', '8', '9']
df['new_col6'] = ['10', '11', '12']
for str_col in ['new_col5','new_col6']:
df[[str_col]] = df[[str_col]].apply(pd.to_numeric)
print(df)
Here is the output:
col3 new_col1 new_col2 new_col3 new_col4 new_col5 new_col6 0 x 0 5 5 7 10 1 y 0 7 7 8 11 2 z 0 9 9 9 12