简体   繁体   English

df.head()有时在Pandas,Python中不起作用

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

I'm a beginner in Python and the Pandas library, and I'm rather confused by some basic functionality of DataFrame. 我是Python和Pandas库的初学者,我对DataFrame的一些基本功能感到困惑。 I've got a pandas DataFrame as below: 我有一个pandas DataFrame如下:

>>>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()函数了:

>>>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. 谢谢。

df.head(n) returns a DataFrame holding the first n rows of df. df.head(n)返回一个DataFrame前n行df的DataFrame 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. 现在默认显示一个DataFrame pandas检查终端的宽度,如果这个太小而无法显示DataFrame ,则会显示摘要视图。 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) ? 您可以通过pd.set_printoptions(max_columns=10)增加终端的大小,还是禁用列上的自动检测?

Try the below code segment: 请尝试以下代码段:

from IPython.display import display
display(df.head())
 DataFrame.head(n=5)

Return the first n rows. 返回前n行。

This function returns the first n rows for the object based on position. 此函数根据位置返回对象的前n行。 It is useful for quickly testing if your object has the right type of data in it. 如果您的对象中包含正确类型的数据,则可以快速测试它。

Parameters: 参数:

n : int, default 5

Number of rows to select. 要选择的行数。

Returns: 返回:

obj_head : type of caller

The first n rows of the caller object. 调用者对象的前n行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM