Pandas – make a column dtype object or Factor

You can use the astype method to cast a Series (one column):

df['col_name'] = df['col_name'].astype(object)

Or the entire DataFrame:

df = df.astype(object)


Since version 0.15, you can use the category datatype in a Series/column:

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

Note: pd.Factor was been deprecated and has been removed in favor of pd.Categorical.

Leave a Comment