Pandas first 5 and last 5 rows in single iloc operation

拥有回忆 提交于 2021-02-05 11:18:26

问题


I need to check df.head() and df.tail() many times. When using df.head(), df.tail() jupyter notebook dispalys the ugly output.

Is there any single line command so that we can select only first 5 and last 5 rows:

something like:
df.iloc[:5 | -5:] ?

Test example:

df = pd.DataFrame(np.random.rand(20,2))
df.iloc[:5]

Update
Ugly but working ways:

df.iloc[(np.where( (df.index < 5) | (df.index > len(df)-5)))[0]]

or,
df.iloc[np.r_[np.arange(5), np.arange(df.shape[0]-5, df.shape[0])]]

回答1:


Try look at numpy.r_

df.iloc[np.r_[0:5, -5:0]]
Out[358]: 
           0         1
0   0.899673  0.584707
1   0.443328  0.126370
2   0.203212  0.206542
3   0.562156  0.401226
4   0.085070  0.206960
15  0.082846  0.548997
16  0.435308  0.669673
17  0.426955  0.030303
18  0.327725  0.340572
19  0.250246  0.162993

Also head + tail is not a bad solution

df.head(5).append(df.tail(5))
Out[362]: 
           0         1
0   0.899673  0.584707
1   0.443328  0.126370
2   0.203212  0.206542
3   0.562156  0.401226
4   0.085070  0.206960
15  0.082846  0.548997
16  0.435308  0.669673
17  0.426955  0.030303
18  0.327725  0.340572
19  0.250246  0.162993



回答2:


df.query("index<5 | index>"+str(len(df)-5))

Here's a way to query the index. You can change the values to whatever you want.




回答3:


Another approach (per this SO post)

  • uses only Pandas .isin()

Generate some dummy/demo data

df = pd.DataFrame({'a':range(10,100)})

print(df.head())
    a
0  10
1  11
2  12
3  13
4  14

print(df.tail())
     a
85  95
86  96
87  97
88  98
89  99

print(df.shape)
(90, 1)

Generate list of required indexes

ls = list(range(5)) + list(range(len(df)-5, len(df)))

print(ls)
[0, 1, 2, 3, 4, 85, 86, 87, 88, 89]

Slice DataFrame using list of indexes

df_first_last_5 = df[df.index.isin(ls)]

print(df_first_last_5)
     a
0   10
1   11
2   12
3   13
4   14
85  95
86  96
87  97
88  98
89  99


来源:https://stackoverflow.com/questions/55699481/pandas-first-5-and-last-5-rows-in-single-iloc-operation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!