Skip to content

Instantly share code, notes, and snippets.

@amalgjose
Last active February 5, 2023 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amalgjose/e8eaae4b813a9c1e16beb03fdb8a785f to your computer and use it in GitHub Desktop.
Save amalgjose/e8eaae4b813a9c1e16beb03fdb8a785f to your computer and use it in GitHub Desktop.
How to convert or change the datatypes of columns in a pandas dataframe ?. For more details, please refer to the blog https://amalgjose.com/2020/05/22/how-to-convert-or-change-the-data-type-of-columns-in-pandas-dataframe
import pandas as pd
# create a sample dataframe
df = pd.DataFrame({'emp_id': ['111', '112', '113'], 'salary': ['40000', '50000', '60000'], 'name':['amal', 'sabitha', 'edward']})
# print the dataframe
print(df)
# print the datatypes in the dataframe
print(df.dtypes)
# now let us convert the data type of salary to integer
df = df.astype({'salary':'int'})
# print the dataframe
print(df)
# print the datatypes in the dataframe
print(df.dtypes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment