2013-02-27 7 views
21

Was ist das Äquivalent zu einer R Factor variable in Python pandas?Ältestes Äquivalent einer Faktorvariablen in Python Pandas

+1

See [diese] (https://groups.google.com/forum/#!msg/pydata/figElKrdwcI/dISL07Id7dUJ). Es scheint, dass das pandas.Factor als Faktorspalten hinzugefügt hat. Aber ich denke nicht, dass dies genau gleich ist, insbesondere bei fehlenden Daten. – agstudy

+1

http://stackoverflow.com/questions/7813132/convert-array-of-string-category-to-array-of-int-from-a-pandas-dataframe – Raufio

Antwort

18

Diese Frage scheint von einem Jahr zurück zu sein, aber da es noch offen ist, hier ist ein Update. Pandas hat categorical dtype und es funktioniert sehr ähnlich factors in R. finden Sie unter diesem Link für weitere Informationen eingeführt:

http://pandas-docs.github.io/pandas-docs-travis/categorical.html

einen Ausschnitt aus dem Link oben Reproduktion zeigt, wie ein „Faktor“ Variable in erstellen Pandas.

In [1]: s = Series(["a","b","c","a"], dtype="category") 

In [2]: s 
Out[2]: 
0 a 
1 b 
2 c 
3 a 
dtype: category 
Categories (3, object): [a < b < c] 
6

Wenn Sie Modellierung usw. machen wollen, viele Leckereien für Faktor innerhalb der patsy library. Ich gebe zu, selbst damit zu kämpfen. Ich fand these slides hilfreich. Ich wünschte, ich könnte ein besseres Beispiel geben, aber das ist soweit ich es mir selbst gemacht habe.

-1
C# array containing category data 
V # array containing numerical data 

H = np.unique(C) 
mydict = {} 
for h in H: 
    mydict[h] = V[C==h] 


boxplot(mydict.values(), labels=mydict.keys())