Python 等价于 R 的 head 和 tail 函数 [英] Python equivalent of R's head and tail function

查看:21
本文介绍了Python 等价于 R 的 head 和 tail 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想预览 Pandas 数据框.我会在 R 中使用 head(mymatrix),但我不知道如何在 Pandas Python 中执行此操作.

I want to preview a Pandas dataframe. I would use head(mymatrix) in R, but I do not know how to do this in Pandas Python.

当我输入

df.head(10) 我明白了...

df.head(10) I get...

<class 'pandas.core.frame.DataFrame'>
Int64Index: 10 entries, 0 to 9
Data columns (total 14 columns):
#Book_Date            10  non-null values
Item_Qty              10  non-null values
Item_id               10  non-null values
Location_id           10  non-null values
MFG_Discount          10  non-null values
Sale_Revenue          10  non-null values
Sales_Flg             10  non-null values
Sell_Unit_Cost        5  non-null values
Store_Discount        10  non-null values
Transaction_Id        10  non-null values
Unit_Cost_Amt         10  non-null values
Unit_Received_Cost    5  non-null values
Unnamed: 0            10  non-null values
Weight                10  non-null values

推荐答案

假设要输出 iris 数据集的前 10 行和后 10 行.

Suppose you want to output the first and last 10 rows of the iris data set.

在 R 中:

data(iris)
head(iris, 10)
tail(iris, 10)

在 Python 中(加载 iris 数据集需要 scikit-learn):

In Python (scikit-learn required to load the iris data set):

import pandas as pd
from sklearn import datasets
iris = pd.DataFrame(datasets.load_iris().data)
iris.head(10)
iris.tail(10)

现在,正如以前回答,如果您的数据框架对于您在终端中使用的显示而言太大,将输出摘要.要在终端中可视化您的数据,您可以扩展终端或减少要显示的列数,如下所示.

Now, as previously answered, if your data frame is too large for the display you use in the terminal, a summary is output. To visualize your data in a terminal, you could either expend the terminal or reduce the number of columns to display, as follows.

iris.iloc[:,1:2].head(10)

编辑.将 .ix 更改为 .iloc.来自熊猫文档

EDIT. Changed .ix to .iloc. From the pandas documentation,

从 0.20.0 开始,.ix 索引器被弃用,取而代之的是更严格的 .iloc 和 .loc 索引器.

Starting in 0.20.0, the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers.

这篇关于Python 等价于 R 的 head 和 tail 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆