how to convert all columns to int in pandas code example
Example 1: convert a pandas column to int
my_series = pd.to_numeric(my_series)
df["a"] = pd.to_numeric(df["a"])
Example 2: astype float across columns pandas
In [273]: cols = df.columns.drop('id')
In [274]: df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')
In [275]: df
Out[275]:
id a b c d e f
0 id_3 NaN 6 3 5 8 1.0
1 id_9 3.0 7 5 7 3 NaN
2 id_7 4.0 2 3 5 4 2.0
3 id_0 7.0 3 5 7 9 4.0
4 id_0 2.0 4 6 4 0 2.0
In [276]: df.dtypes
Out[276]:
id object
a float64
b int64
c int64
d int64
e int64
f float64
dtype: object
Example 3: astype float across columns pandas
cols = df.columns[df.dtypes.eq('object')]