在 Pandas 中,如何将DataFrame的列转换为dtype对象?
还是更好,成为一个因素? (对于那些讲R的人,在Python中,我如何as.factor()?)

另外,pandas.Factorpandas.Categorical有什么区别?

最佳答案

您可以使用 astype 方法强制转换Series(一个列):

df['col_name'] = df['col_name'].astype(object)
或整个DataFrame:
df = df.astype(object)

更新
系列/列中的Since version 0.15, you can use the category datatype:
df['col_name'] = df['col_name'].astype('category')
注意:不赞成使用pd.Factor,并且为了支持pd.Categorical而已将其删除。

关于python - Pandas -使列dtype对象或Factor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15723628/

10-12 18:12
查看更多