how to change datatype of column in pandas code example
Example 1: convert column to numeric pandas
df = df.apply(pd.to_numeric)
df[["a", "b"]] = df[["a", "b"]].apply(pd.to_numeric)
Example 2: python: transform as type numeirc
df['myvar'] = df['myvar'].astype(str)
df['myvar'] = df['myvar'].astype(float)
df['myvar'] = df['myvar'].astype(int)
Example 3: change dataframe column type
>>> df.astype({'col1': 'int32'}).dtypes
col1 int32
col2 int64
dtype: object
Example 4: 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 5: set type of column pandas
df.astype(int)
Example 6: set column datatype pandas
df = pd.read_csv("weather.tsv", sep="\t",
dtype={'Day': str,'Wind':int64})
df.dtypes