【发布时间】:2015-02-22 14:15:41
【问题描述】:
我只知道describe() 函数。还有其他类似str()、summary()、head()的功能吗?
【问题讨论】:
我只知道describe() 函数。还有其他类似str()、summary()、head()的功能吗?
【问题讨论】:
在 pandas 中,info() 方法会创建与 R 的 str() 非常相似的输出:
> str(train)
'data.frame': 891 obs. of 13 variables:
$ PassengerId: int 1 2 3 4 5 6 7 8 9 10 ...
$ Survived : int 0 1 1 1 0 0 0 0 1 1 ...
$ Pclass : int 3 1 3 1 3 3 1 3 3 2 ...
$ Name : Factor w/ 891 levels "Abbing, Mr. Anthony",..: 109 191 358 277 16 559 520 629 417 581 ...
$ Sex : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
$ Age : num 22 38 26 35 35 NA 54 2 27 14 ...
$ SibSp : int 1 1 0 1 0 0 0 3 0 1 ...
$ Parch : int 0 0 0 0 0 0 0 1 2 0 ...
$ Ticket : Factor w/ 681 levels "110152","110413",..: 524 597 670 50 473 276 86 396 345 133 ...
$ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
$ Cabin : Factor w/ 148 levels "","A10","A14",..: 1 83 1 57 1 1 131 1 1 1 ...
$ Embarked : Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ...
$ Child : num 0 0 0 0 0 NA 0 1 0 1 ...
train.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
PassengerId 891 non-null int64
Survived 891 non-null int64
Pclass 891 non-null int64
Name 891 non-null object
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Ticket 891 non-null object
Fare 891 non-null float64
Cabin 204 non-null object
Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.6+ KB
【讨论】:
这提供了类似于 R 的str() 的输出。它呈现唯一值而不是初始值。
def rstr(df): return df.shape, df.apply(lambda x: [x.unique()])
print(rstr(iris))
((150, 5), sepal_length [[5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.4, 4.8, 4.3,...
sepal_width [[3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 2.9, 3.7,...
petal_length [[1.4, 1.3, 1.5, 1.7, 1.6, 1.1, 1.2, 1.0, 1.9,...
petal_width [[0.2, 0.4, 0.3, 0.1, 0.5, 0.6, 1.4, 1.5, 1.3,...
class [[Iris-setosa, Iris-versicolor, Iris-virginica]]
dtype: object)
【讨论】:
summary() ~ describe()
head() ~ head()
我不确定 str() 的等效项。
【讨论】:
head()?您的意思是.head() 方法仅适用于少数数据类型吗?
Pandas 提供了广泛的Comparison with R / R libraries。最明显的区别是 R 更喜欢函数式编程,而 Pandas 是面向对象的,以数据框为关键对象。 R 和 Python 的另一个区别是 Python 从 0 开始数组,而 R 从 1 开始。
R | Pandas
-------------------------------
summary(df) | df.describe()
head(df) | df.head()
dim(df) | df.shape
slice(df, 1:10) | df.iloc[:9]
【讨论】:
对于与 R 中的 str() 函数等效的 Python,我使用方法 dtypes。这将为每一列提供数据类型。
In [22]: df2.dtypes
Out[22]:
Survived int64
Pclass int64
Sex object
Age float64
SibSp int64
Parch int64
Ticket object
Fare float64
Cabin object
Embarked object
dtype: object
【讨论】:
我还是更喜欢str(),因为它列出了一些例子。 info 的一个令人困惑的方面是它的行为取决于一些环境设置,例如 pandas.options.display.max_info_columns。
我认为最好的替代方法是调用 info 并使用其他一些会强制执行固定行为的参数:
df.info(null_counts=True, verbose=True)
对于您的其他功能:
summary(df) | df.describe()
head(df) | df.head()
dim(df) | df.shape
【讨论】:
我对 R 了解不多,但这里有一些线索:
str =>
困难的一个...对于您可以使用 dir() 的函数,数据集上的 dir() 将为您提供所有方法,所以也许这不是您想要的...
summary => describe.
查看参数以自定义结果。
head => your can use head(), or use slices.
像你已经做的那样。获取名为 ds ds[:10] 的数据集的前 10 行,与 tail ds[:-10] 相同
【讨论】: