Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

df.head() sometimes doesn't work in Pandas, Python

Tags:

I'm a beginner in Python and the Pandas library, and I'm rather confused by some basic functionality of DataFrame. I've got a pandas DataFrame as below:

>>>df.head()  
              X  Y       unixtime
0  652f5e69fcb3  1  1346689910622
1        400292  1  1346614723542
2  1c9d02e4f14e  1  1346862070161
3        610449  1  1346806384518
4        207664  1  1346723370096

However, after I performed some function:

def unixTodate(unix):
  day = dt.datetime.utcfromtimestamp(unix/1000).strftime('%Y-%m-%d')
  return day

df['day'] = df['unixtime'].apply(unixTodate)

I could no longer make use of the df.head() function:

>>>df.head()  

<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 190648 to 626582
Data columns:
X              5  non-null values
Y              5  non-null values
unixtime       5  non-null values
day            5  non-null values
dtypes: int64(3), object(5)

I can't see why this is happening. Am I doing something wrong here? Any pointer is welcome! Thanks.

like image 858
S.zhen Avatar asked Oct 26 '12 11:10

S.zhen


People also ask

What does head () do in Panda?

pandas. head () function is used to access the first n rows of a dataframe or series. It returns a smaller version of the caller object with the first few entries.

How do I check my head in pandas?

head() The head() returns the first n rows for the object based on position. If your object has the right type of data in it, it is useful for quick testing. This method is used for returning top n (by default value 5) rows of a data frame or series.

Is Head () a method or function in Python?

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].

Why are Python pandas not working?

ModuleNotFoundError: No module named 'pandas' In most cases this error in Python generally raised: You haven't installed Pandas explicitly with pip install pandas. You may have different Python versions on your computer and Pandas is not installed for the particular version you're using.


2 Answers

df.head(n) returns a DataFrame holding the first n rows of df. Now to display a DataFrame pandas checks by default the width of the terminal, if this is too small to display the DataFrame a summary view will be shown. Which is what you get in the second case.

Could you increase the size of your terminal, or disable autodetect on the columns by pd.set_printoptions(max_columns=10)?

like image 193
Wouter Overmeire Avatar answered Oct 24 '22 18:10

Wouter Overmeire


Try the below code segment:

from IPython.display import display
display(df.head())
like image 30
Kokul Jose Avatar answered Oct 24 '22 18:10

Kokul Jose