Python 蟒蛇熊猫选择头部和尾部

Python 蟒蛇熊猫选择头部和尾部,python,pandas,slice,tail,head,Python,Pandas,Slice,Tail,Head,对于熊猫中的数据帧,如何选择前5个值和后5个值 比如说 In [11]: df Out[11]: A B C 2012-11-29 0 0 0 2012-11-30 1 1 1 2012-12-01 2 2 2 2012-12-02 3 3 3 2012-12-03 4 4 4 2012-12-04 5 5 5 2012-12-05 6 6 6 2012-12-06 7 7 7 2012-12-07 8 8 8 2012

对于熊猫中的数据帧,如何选择前5个值和后5个值

比如说

In [11]: df
Out[11]: 
        A  B  C
2012-11-29  0  0  0
2012-11-30  1  1  1
2012-12-01  2  2  2
2012-12-02  3  3  3
2012-12-03  4  4  4
2012-12-04  5  5  5
2012-12-05  6  6  6
2012-12-06  7  7  7
2012-12-07  8  8  8
2012-12-08  9  9  9
如何显示前两行和后两行?

您可以使用:


您可以使用
df.head(5)
df.tail(5)
获得前五个和后五个。 或者,您可以创建新的数据框和
append()
head和tail:

new_df = df.tail(5)
new_df = new_df.append(df.head(5))
import pandas as pd
from string import ascii_lowercase, ascii_uppercase

df = pd.DataFrame(
    {"upper": list(ascii_uppercase), "lower": list(ascii_lowercase)}, index=range(1, 27)
)

df.apply(lambda x: pd.concat([x.head(2), x.tail(2)]))


   upper lower
1      A     a
2      B     b
25     Y     y
26     Z     z
您可以使用df.head(2)和df.tail(2)

小的简单功能:

def ends(df, x=5):
    return df.head(x).append(df.tail(x))
def display_n(df,n): 
    with pd.option_context('display.max_rows',n*2):
        display(df)
这样使用:

df = pd.DataFrame(np.random.rand(15,6))
ends(df,2)
import numpy as np
df = pd.DataFrame(np.random.rand(15,6))
df.ends(2)
事实上,我经常使用它,我认为这将是一个伟大的功能添加到熊猫。(不向pandas.DataFrame核心API添加任何功能)我在导入后添加它,如下所示:

import pandas as pd
def ends(df, x=5):
    return df.head(x).append(df.tail(x))
setattr(pd.DataFrame,'ends',ends)
像这样使用:

df = pd.DataFrame(np.random.rand(15,6))
ends(df,2)
import numpy as np
df = pd.DataFrame(np.random.rand(15,6))
df.ends(2)

与Linas Fx相关

定义如下

pd.DataFrame.less = lambda df, n=10: df.head(n//2).append(df.tail(n//2))
然后只能键入
df.less()

它与type
df.head().append(df.tail())

如果您键入
df.less(2)
,结果与
df.head(1).append(df.tail(1))
不是完全相同的问题,但是如果您只想显示顶部/底部5行(例如在jupyter中显示
或常规
打印
),如果您使用上下文,可能有一种更简单的方法

#生成100个3d随机数
df=pd.DataFrame(np.random.randn(100,3))
#按轴和对它们进行排序
df=df.loc[df.sum(轴=1.index]
使用pd.option\u上下文('display.max\u rows',10):
打印(df)
产出:

0112
0  -0.649105 -0.413335  0.374872
1   3.390490  0.552708 -1.723864
2  -0.781308 -0.277342 -0.903127
3   0.433665 -1.125215 -0.290228
4  -2.028750 -0.083870 -0.094274
..       ...       ...       ...
95  0.443618 -1.473138  1.132161
96 -1.370215 -0.196425 -0.528401
97  1.062717 -0.997204 -1.666953
98  1.303512  0.699318 -0.863577
99 -0.109340 -1.330882 -1.455040
[100行x 3列]

在Jupyter中,我们将扩展@BOCKUP的答案,创建一个可重用的便利功能:

def ends(df, x=5):
    return df.head(x).append(df.tail(x))
def display_n(df,n): 
    with pd.option_context('display.max_rows',n*2):
        display(df)
然后

返回

         0           1           2
0        0.167961    -0.732745   0.952637
1        -0.050742   -0.421239   0.444715
...      ...         ...         ...
98       0.085264    0.982093    -0.509356
99       -0.758963   -0.578267   -0.115865
(格式良好的HTML表格除外)

当df是
df=pd.DataFrame(np.random.randn(100,3))

注:

  • 当然,您可以通过将上面的
    display
    修改为
    print
    来打印与文本相同的内容
  • 在类似unix的系统上,您可以将上述功能自动加载到所有笔记本电脑中,方法是将其放入
    ~/.ipython/profile\u default/startup
    中的
    py
    ipy
    文件中

  • 为此,您应该同时使用
    head()
    tail()
    。我认为最简单的方法是:

    df.head(5).append(df.tail(5))
    

    如果要将其仅保留为熊猫,可以使用
    apply()
    连接头部和尾部:

    new_df = df.tail(5)
    new_df = new_df.append(df.head(5))
    
    import pandas as pd
    from string import ascii_lowercase, ascii_uppercase
    
    df = pd.DataFrame(
        {"upper": list(ascii_uppercase), "lower": list(ascii_lowercase)}, index=range(1, 27)
    )
    
    df.apply(lambda x: pd.concat([x.head(2), x.tail(2)]))
    
    
       upper lower
    1      A     a
    2      B     b
    25     Y     y
    26     Z     z
    

    你的问题没有意义,你说你要选择前5个值和最后5个值,你指的是行还是单独的值?请显示除了其他值之外的期望输出,头和尾可以链接(如在BASH中),以便在中间给你一个值(DF.头(90)。尾(10))为了得到80到90的值,Duplicate answer以几种方式扩展了这个答案…你能把它提交给pandas git吗?这应该是一个默认函数