Method #1: Using DataFrame.astype()
# importing pandas as pd
import pandas as pd
# sample dataframe
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e'],
'C': [1.1, '1.0', '1.3', 2, 5] })
# converting all columns to string type
df = df.astype(str)
print(df.dtypes)
# importing pandas as pd
import pandas as pd
# sample dataframe
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e'],
'C': [1.1, '1.0', '1.3', 2, 5] })
# using dictionary to convert specific columns
convert_dict = {'A': int,
'C': float
}
df = df.astype(convert_dict)
print(df.dtypes)
Method #2: Using DataFrame.apply()
We can pass pandas.to_numeric
, pandas.to_datetime
and pandas.to_timedelta
as argument to apply() function to change the datatype of one or more columns to numeric, datetime and timedelta respectively.
# importing pandas as pd
import pandas as pd
# sample dataframe
df = pd.DataFrame({
'A': [1, 2, 3, '4', '5'],
'B': ['a', 'b', 'c', 'd', 'e'],
'C': [1.1, '2.1', 3.0, '4.1', '5.1'] })
# using apply method
df[['A', 'C']] = df[['A', 'C']].apply(pd.to_numeric)
print(df.dtypes)
Method #3: Using DataFrame.infer_objects()
# importing pandas as pd
import pandas as pd
# sample dataframe
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': ['a', 'b', 'c', 'd', 'e'],
'C': [1.1, 2.1, 3.0, 4.1, 5.1]
}, dtype ='object')
# converting datatypes
df = df.infer_objects()
print(df.dtypes)
References
https://www.geeksforgeeks.org/change-data-type-for-one-or-more-columns-in-pandas-dataframe/
https://towardsdatascience.com/my-pandas-cheat-sheet-b71437ab26f