Last active
February 5, 2023 02:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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