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 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)
EDIT. Changed .ix to .iloc. From the pandas documentation,
Answer from essicolo on Stack OverflowStarting in 0.20.0, the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers.
Python equivalent of R's head and tail function - Stack Overflow
df.head(n) - Why does it exist?
pandas - What does !head do in python and NumPy? - Stack Overflow
pandas - Easiest way to print the head of a data in python? - Stack Overflow
Videos
Can somebody please explain why the head() function in pandas exists. Since it only returns self.iloc[:n], why was it implemented in the first place?
I use df.head(n) all the time. But I just looked into the source code an realized that there is no "extra value" that is being added. Just a bunch of kinda redunant lines in the code. Is there a reason why it is not redundant?
Sorry for the nooby question and thanks to all serious replies :)
It has nothing to do with Python or NumPy specifically; that's a feature of ipython to run external programs, in this case, the head utility from the standard suite of *NIX command line utilities. head -4 just reads the first four lines of the file provided and echoes them to the terminal.
Using "!" means that you want to call a system command. If you are on a Linux/Unix system (Google Colab uses such a system for instance) then you can call Linux/Unix commands directly using "!". Looks like you are using a Windows system and the command "head" does not exist as a command in Windows. Assuming that you are using a locally hosted Jupyter Notebook, then it is running on a Windows system.
The head command itself is a command-line utility for outputting the first part of files given to it via standard input. It writes results to standard output. By default head returns the first ten lines of each file that it is given.