对于因数,python/pandas 与 R/caret 的 str 函数的直接等价物是什么?
What is the direct python/pandas equivalent of R/caret's str function, for factors?
我特别想要一个显示 factor/categorical 变量级别的函数,就像 R/caret 中的 str() 一样。
例如,在 R 中我们有:
data(iris)
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
我如何在 python/pandas 中准确地完成此操作?
dataFrame.describe()
和 dataFrame.info()
都不适合 iris$Species
。
最简单的是
df.Species.value_counts()
我特别想要一个显示 factor/categorical 变量级别的函数,就像 R/caret 中的 str() 一样。
例如,在 R 中我们有:
data(iris)
str(iris)
## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
我如何在 python/pandas 中准确地完成此操作?
dataFrame.describe()
和 dataFrame.info()
都不适合 iris$Species
。
最简单的是
df.Species.value_counts()