Pandas - make a column dtype object or Factor

pandas, python

Solution

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)

Update

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`.

Problem

In pandas, how can I convert a column of a DataFrame into dtype object? Or better yet, into a factor? (For those who speak R, in Python, how do I `as.factor()`?) Also, what's the difference between `pandas.Factor` and `pandas.Categorical`?

Original source