pandas显示前n行和最后n行

目录

Exploring data frames

Get help on Jupyter notebook

创建dataframe

显示前n行

显示后n行

外传

我是总结


Exploring data frames

本文主要介绍pandas中的head和tail函数, 来查询数据

  • jupyter notebook 简单使用
  • pandas.head
  • pandas.tail

Get help on Jupyter notebook

如果想要查看类的属性或者方法名, 可以使用help和?

  • object_name?
  • help(object_name)

创建dataframe

本文将以下面的DataFrame进行讲解

import numpy as np
import pandas as pd

df = pd.DataFrame({'name': ['Tom', 'Sam', 'Steve', 'David', 'Simon', 'Angel'],
                   'ages': [18, 19, 20, 21, 22, 23]})

print(df)
'''

    name  ages
0    Tom    18
1    Sam    19
2  Steve    20
3  David    21
4  Simon    22
5  Angel    23
'''

显示前n行

df.head
先看下head的说明

In: df.head?

Signature: df.head(n: int=5) -> ~FrameOrSeries
Docstring:
Return the first `n` rows.

This function returns the first `n` rows for the object based
on position. It is useful for quickly testing if your object
has the right type of data in it.

For negative values of `n`, this function returns all rows except
the last `n` rows, equivalent to ``df[:-n]``.

Parameters
----------
n: int, default 5
    Number of rows to select.

Returns
-------
same type as caller
    The first `n` rows of the caller object.

See Also
--------
DataFrame.tail: Returns the last `n` rows.

Examples
--------
>> > df = pd.DataFrame({'animal': ['alligator', 'bee', 'falcon', 'lion',
                                   ...                    'monkey', 'parrot', 'shark', 'whale', 'zebra']})
>> > df
    animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
5     parrot
6      shark
7      whale
8      zebra

Viewing the first 5 lines

>> > df.head()
    animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey

Viewing the first `n` lines(three in this case)

>> > df.head(3)
    animal
0  alligator
1        bee
2     falcon

For negative values of `n`

>> > df.head(-3)
    animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
5     parrot

方法一: 显示前3行

方法二: 显示前3行

显示后n行

先看下tail的说明

Signature: df.tail(n:int=5) -> ~FrameOrSeries
Docstring:
Return the last `n` rows.

This function returns last `n` rows from the object based on
position. It is useful for quickly verifying data, for example,
after sorting or appending rows.

For negative values of `n`, this function returns all rows except
the first `n` rows, equivalent to ``df[n:]``.

Parameters
----------
n : int, default 5
    Number of rows to select.

Returns
-------
type of caller
    The last `n` rows of the caller object.

See Also
--------
DataFrame.head : The first `n` rows of the caller object.

Examples
--------
>>> df = pd.DataFrame({'animal': ['alligator', 'bee', 'falcon', 'lion',
...                    'monkey', 'parrot', 'shark', 'whale', 'zebra']})
>>> df
      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
5     parrot
6      shark
7      whale
8      zebra

Viewing the last 5 lines

>>> df.tail()
   animal
4  monkey
5  parrot
6   shark
7   whale
8   zebra

Viewing the last `n` lines (three in this case)

>>> df.tail(3)
  animal
6  shark
7  whale
8  zebra

For negative values of `n`

>>> df.tail(-3)
   animal
3    lion
4  monkey
5  parrot
6   shark
7   whale
8   zebra

方法一: 显示最后2行

方法二: 显示最后2行

外传

到这里, 我们已经学会显示前n行和最后n行.那我们如何知道文件有多少行呢?

  • 方案一: 用肉眼数(不推荐)
  • 方案二: df.shape
  • 方案三: len(df)

我是总结

  • df.shape 获取rows和cols
  • df.head(n) 显示前n行
  • df.tail(n) 显示最后n行

                                                              扫码关注公众号

                                                                    扫码关注公众号: 风起帆扬了
                                                                        来一起学习,成长,分享
                                                                          航行在测试的大道上
                                                                               喜欢就点赞吧

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值