pandas python pandas选择头部和尾部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42504984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pandas select both head and tail
提问by fu xue
For a DataFrame in Pandas,How can select both the first 5 values and last 5 values? For example
对于 Pandas 中的 DataFrame,如何选择前 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
How to show the first tow row and the last tow rows?
如何显示第一个拖行和最后一个拖行?
回答by jezrael
You can use ilocwith numpy.r_:
print (np.r_[0:2, -2:0])
[ 0 1 -2 -1]
df = df.iloc[np.r_[0:2, -2:0]]
print (df)
A B C
2012-11-29 0 0 0
2012-11-30 1 1 1
2012-12-07 8 8 8
2012-12-08 9 9 9
df = df.iloc[np.r_[0:4, -4:0]]
print (df)
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-05 6 6 6
2012-12-06 7 7 7
2012-12-07 8 8 8
2012-12-08 9 9 9
回答by Linas Fx
You can use df.head(5)and df.tail(5)to get first five and last five.
Optionally you can create new data frame and append()head and tail:
您可以使用df.head(5)和df.tail(5)获得前五个和最后五个。您可以选择创建新的数据框append()和头尾:
new_df = df.tail(5)
new_df = new_df.append(df.head(5))
回答by Bolster
Not quitethe same question but if you just want to showthe top / bottom 5 rows (eg with displayin jupyter or regular print, there's potentially a simpler way than this if you use the pd.option_contextcontext.
不是完全相同的问题,但如果您只想显示顶部/底部 5 行(例如display在 jupyter 或 regular 中print,如果您使用pd.option_context上下文,可能有比这更简单的方法。
#make 100 3d random numbers
df = pd.DataFrame(np.random.randn(100,3))
# sort them by their axis sum
df = df.loc[df.sum(axis=1).index]
with pd.option_context('display.max_rows',10):
print(df)
Outputs:
输出:
0 1 2
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 rows x 3 columns]
回答by ic_fl2
Small simple function:
小简单功能:
def ends(df, x=5):
return df.head(x).append(df.tail(x))
And use like so:
并像这样使用:
df = pd.DataFrame(np.random.rand(15,6))
ends(df,2)
I actually use this so much, I think it would be a great feature to add to pandas.(No features are to be added to pandas.DataFrame core API) I add it after import like so:
我真的用了这么多, 我认为将其添加到Pandas中将是一个很棒的功能。(不向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)
Use like so:
像这样使用:
import numpy as np
df = pd.DataFrame(np.random.rand(15,6))
df.ends(2)
回答by watsonic
In Jupyter, expanding on @bolster's answer, we'll create a reusable convenience function:
在Jupyter 中,扩展@bolster的答案,我们将创建一个可重用的便利功能:
def display_n(df,n):
with pd.option_context('display.max_rows',n*2):
display(df)
Then
然后
display_n(df,2)
Returns
退货
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
(except as a nicely formatted HTML table)
(除了作为格式良好的 HTML 表格)
when df is df = pd.DataFrame(np.random.randn(100,3))
当 df 是 df = pd.DataFrame(np.random.randn(100,3))
Notes:
笔记:
- Of course you could make the same thing print as text by modifying
displaytoprintabove. - On unix-like systems, you can the autoload the above function in all notebooks by placing it in a
pyoripyfile in~/.ipython/profile_default/startupas described here.
- 当然,你可以通过修改使事情印刷文字相同
display,以print上面。 - 在类Unix系统中,你可以在所有的笔记本电脑通过将其放置在一个自动加载上述功能
py或ipy文件中~/.ipython/profile_default/startup所描述这里。
回答by You Oneandzero
Associated with Linas Fx.
与 Linas Fx 相关。
Defining below
下面定义
pd.DataFrame.less = lambda df, n=10: df.head(n//2).append(df.tail(n//2))
then you can type only df.less()
那么你只能输入 df.less()
It's same as type df.head().append(df.tail())
它与类型相同 df.head().append(df.tail())
If you type df.less(2), the result is same as df.head(1).append(df.tail(1))
如果您键入df.less(2),则结果与df.head(1).append(df.tail(1))
回答by Subhash Moond
you can uses df.head(2) and df.tail(2)
你可以使用 df.head(2) 和 df.tail(2)

