transform column of dataframe to categorical in python

To transform a column of a dataframe to categorical in Python, we can use the astype() function and pass category as the argument to convert the column to a categorical datatype. Here's an example:

main.py
import pandas as pd

# create a sample dataframe
df = pd.DataFrame({
    'fruit': ['apple', 'banana', 'orange', 'apple', 'orange'],
    'number': [1, 2, 3, 1, 2]
})

# transform the 'fruit' column to categorical
df['fruit'] = df['fruit'].astype('category')

# print the data types of the columns
print(df.dtypes)
313 chars
14 lines

Output:

main.py
fruit     category
number       int64
dtype: object
52 chars
4 lines

As you can see, the fruit column is now of datatype category.

gistlibby LogSnag