Python equivalent of R's head and tail function Python equivalent of R's head and tail function r r

Python equivalent of R's head and tail function


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

In R:

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

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

import pandas as pdfrom sklearn import datasetsiris = 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)

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

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