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
The head() function is used to get the first n rows. It is helpful for quickly testing if your object has the right type of data in it. For negative values of n , the head() function returns all rows except the last n rows, equivalent to df[:-n].
head() Returns the first n rows. 8. tail() Returns the last n rows.
The head function in Python displays the first five rows of the dataframe by default. It takes in a single parameter: the number of rows. We can use this parameter to display the number of rows of our choice.
import random flips = 0 heads = 0 tails = 0 while flips < 100: flips += 1 coin = random. randint(1, 2) if coin == 1: print("Heads") heads += 1 else: print("Tails") tails += 1 total = flips print(total, "total flips.") print("With a total of,", heads, "heads and", tails, "tails.")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With