24

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.

When I type

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
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
  • 2
    http://pandas.pydata.org/pandas-docs/stable/basics.html#head-and-tail – BrenBarn Aug 08 '14 at 19:55
  • 1
    The problem with the above is that this shows me only the column names still and tells me 5 obs. I want to see structure output immediately, like head() in R. – wolfsatthedoor Aug 08 '14 at 19:59
  • 1
    I haven't used R, so I'm not sure what you mean by structure output. Calling `head` or `tail` on a dataframe will return another dataframe, so you can do all sorts of manipulation on that. – Shashank Agarwal Aug 08 '14 at 20:23
  • Is the output truncated is that the issue? – EdChum Aug 08 '14 at 20:24
  • 1
    What do you mean "structure output immediately"? `head` in Pandas shows you the first few rows of the DataFrame, just like in R. Can you show an example of the output you're getting and explain what you want instead? – BrenBarn Aug 08 '14 at 20:28
  • It shows me column names only, is it because I have too many columns? (I have 21) or the data set is too big? Because it does NOT show me what R would. – wolfsatthedoor Aug 08 '14 at 21:30
  • 2
    Upgrading Pandas to >= 0.13 should fix this. – JaminSore Aug 08 '14 at 21:47
  • Do I need to do something special besides import pandas to get that? It appears so given that I'm not automatically getting the updated one. – wolfsatthedoor Aug 08 '14 at 21:48
  • Yes, you would have to download and [install](http://pandas.pydata.org/pandas-docs/stable/install.html) newer version. You can also change print options ([see this question](http://stackoverflow.com/questions/11707586/python-pandas-widen-output-display)) – JaminSore Aug 08 '14 at 22:02
  • Upgrading fixed this. – wolfsatthedoor Mar 22 '15 at 15:34

1 Answers1

25

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,

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

essicolo
  • 803
  • 7
  • 13