2 Upvotes

Converting Data Types

Python
Data Preprocessing

Shows how to convert dataframe column to different data types. We see how to convert to string, integer, float and datetime.

#GENERAL SYNTAX
df[column_name] = df[column_name].astype(data type)

#CONVERTING TO STRING
df[column_name] = df[column_name].astype(str)

#CONVERTING TO INTEGER
df[column_name] = df[column_name].astype(int)

#CONVERTING TO FLOAT
df[column_name] = df[column_name].astype(float)

#CONVERTING TO DATETIME
df[column_name] = pd.to_datetime(df[column_name] )

By analyseup - Last Updated Jan. 17, 2022, 11:10 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
2
Top Contributors
98