Python 相当于 R 的 head 和 tail 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25211220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:53:09  来源:igfitidea点击:

Python equivalent of R's head and tail function

pythonrviewpandas

提问by wolfsatthedoor

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.

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

When I type

当我打字

df.head(10) I get...

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

<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

采纳答案by essicolo

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

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

In R:

在 R 中:

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

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

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

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.ix[:,1:2].head(10)