Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Data Science by (18.4k points)

I want to convert a particular column of my data frame into a string type. I am using a housing dataset and I wanted to convert my zipcode column to string data type. I want to do this because when I use the linear regression it considers as a categorical variable not numerical

df = pd.DataFrame({'zipcode': {17384: 98125, 2680: 98107, 722: 98005, 18754: 98109, 14554: 98155}, 'bathrooms': {17384: 1.5, 2680: 0.75, 722: 3.25, 18754: 1.0, 14554: 2.5}, 'sqft_lot': {17384: 1650, 2680: 3700, 722: 51836, 18754: 2640, 14554: 9603}, 'bedrooms': {17384: 2, 2680: 2, 722: 4, 18754: 2, 14554: 4}, 'sqft_living': {17384: 1430, 2680: 1440, 722: 4670, 18754: 1130, 14554: 3180}, 'floors': {17384: 3.0, 2680: 1.0, 722: 2.0, 18754: 1.0, 14554: 2.0}})

print (df)

       bathrooms  bedrooms  floors  sqft_living  sqft_lot  zipcode

722         3.25         4     2.0         4670     51836    98005

2680        0.75         2     1.0         1440      3700    98107

14554       2.50         4     2.0         3180      9603    98155

17384       1.50         2     3.0         1430      1650    98125

18754       1.00         2     1.0         1130      2640    98109

1 Answer

0 votes
by (36.8k points)

You need astype:

df['zipcode'] = df.zipcode.astype(str)

#df.zipcode = df.zipcode.astype(str)

For converting to categorical:

df['zipcode'] = df.zipcode.astype('category')

#df.zipcode = df.zipcode.astype('category')

Another solution is Categorical:

df['zipcode'] = pd.Categorical(df.zipcode)

Sample with data:

import pandas as pd

df = pd.DataFrame({'zipcode': {17384: 98125, 2680: 98107, 722: 98005, 18754: 98109, 14554: 98155}, 'bathrooms': {17384: 1.5, 2680: 0.75, 722: 3.25, 18754: 1.0, 14554: 2.5}, 'sqft_lot': {17384: 1650, 2680: 3700, 722: 51836, 18754: 2640, 14554: 9603}, 'bedrooms': {17384: 2, 2680: 2, 722: 4, 18754: 2, 14554: 4}, 'sqft_living': {17384: 1430, 2680: 1440, 722: 4670, 18754: 1130, 14554: 3180}, 'floors': {17384: 3.0, 2680: 1.0, 722: 2.0, 18754: 1.0, 14554: 2.0}})

print (df)

       bathrooms  bedrooms  floors  sqft_living  sqft_lot  zipcode

722         3.25         4     2.0         4670     51836    98005

2680        0.75         2     1.0         1440      3700    98107

14554       2.50         4     2.0         3180      9603    98155

17384       1.50         2     3.0         1430      1650    98125

18754       1.00         2     1.0         1130      2640    98109

print (df.dtypes)

bathrooms      float64

bedrooms         int64

floors         float64

sqft_living      int64

sqft_lot         int64

zipcode          int64

dtype: object

df['zipcode'] = df.zipcode.astype('category')

print (df)

       bathrooms  bedrooms  floors  sqft_living  sqft_lot zipcode

722         3.25         4     2.0         4670     51836   98005

2680        0.75         2     1.0         1440      3700   98107

14554       2.50         4     2.0         3180      9603   98155

17384       1.50         2     3.0         1430      1650   98125

18754       1.00         2     1.0         1130      2640   98109

print (df.dtypes)

bathrooms       float64

bedrooms          int64

floors          float64

sqft_living       int64

sqft_lot          int64

zipcode        category

dtype: object

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...