how to convert type of column in pandas code example
Example 1: convert pandas column type
df = df.apply(pd.to_numeric)
df[["a", "b"]] = df[["a", "b"]].apply(pd.to_numeric)
Example 2: change dataframe column type
>>> df.astype({'col1': 'int32'}).dtypes
col1 int32
col2 int64
dtype: object
Example 3: set dtype for multiple columns pandas
import pandas as pd
df = pd.DataFrame({'id':['a1', 'a2', 'a3', 'a4'],
'A':['0', '1', '2', '3'],
'B':['1', '1', '1', '1'],
'C':['0', '1', '1', '0']})
df[['A', 'B', 'C']] = df[['A', 'B', 'C']].apply(pd.to_numeric, axis = 1)
Example 4: set column datatype pandas
df = pd.read_csv("weather.tsv", sep="\t",
dtype={'Day': str,'Wind':int64})
df.dtypes