August W. Gruneisen
August W. Gruneisen

Reputation: 227

How to show first+last N rows of a DataFrame

I am looking for the best way to quickly preview the first and last N rows of a DataFrame at the same time.

I realize I can use df.head(N) and df.tail(N) separately but this returns two DataFrames. Is it best to simply call these 2 separate functions and concatenate the two, or is there a simpler method for this?

Current:

df.head(5)     # first 5 rows
df.tail(5)     # last 5 rows

Desired:

df.headtail(5)     # 10 rows total

Would like to display the first and last 5 rows as a single DataFrame for quick preview of the 'range' of my dataset.

Upvotes: 7

Views: 19828

Answers (4)

Life is complex
Life is complex

Reputation: 15639

pandas < 1.4.0

import pandas as pd

df = pd.DataFrame({'item': range(100)})
sliced_df = df.head(5)
sliced_df = sliced_df.append(df.tail(5))
print (sliced_df)
# output 
    item
0     0
1     1
2     2
3     3
4     4
95   95
96   96
97   97
98   98
99   99

append was deprecated in pandas version 1.4.0, so concat is now used.

pandas => 1.4.0

import pandas as pd

df = pd.DataFrame({'item': range(100)})
sliced_df = pd.concat([df.head(5), df.tail(5)])
print(sliced_df)
# output 
    item
0     0
1     1
2     2
3     3
4     4
95   95
96   96
97   97
98   98
99   99

Upvotes: 4

mapping dom
mapping dom

Reputation: 1955

Another option to show first and last n rows of pandas data frame in Python 3+ using Numpy range. In this example we retrieve the first and last 5 rows of the data frame. This is same in approach to the answer by Andy L

df.iloc[np.r_[0:5, -5:0]]

Upvotes: 3

Andy L.
Andy L.

Reputation: 25269

Simple with .iloc and construct a slice from range

Sample:

np.random.seed(0)
df = pd.DataFrame(np.random.randint(0, 999, 100).reshape(20,5), columns=list('ABCDE'))

Out[240]:
      A    B    C    D    E
0   684  559  629  192  835
1   763  707  359    9  723
2   277  754  804  599   70
3   472  600  396  314  705
4   486  551   87  174  600
5   849  677  537  845   72
6   777  916  115  976  755
7   709  847  431  448  850
8    99  984  177  755  797
9   659  147  910  423  288
10  961  265  697  639  544
11  543  714  244  151  675
12  510  459  882  183   28
13  802  128  128  932   53
14  901  550  488  756  273
15  335  388  617   42  442
16  543  888  257  321  937
17   57  291  870  119  779
18  430   82   91  896  398
19  611  565  908  633  938

head_tail_slice = list(range(5))+list(range(-5,0))

df.iloc[head_tail_slice]

Out[242]:
      A    B    C    D    E
0   684  559  629  192  835
1   763  707  359    9  723
2   277  754  804  599   70
3   472  600  396  314  705
4   486  551   87  174  600
15  335  388  617   42  442
16  543  888  257  321  937
17   57  291  870  119  779
18  430   82   91  896  398
19  611  565  908  633  938

Upvotes: 2

Marat
Marat

Reputation: 15738

print(df.to_string(max_rows=10))

Example:

>>> df = pd.DataFrame({'one': range(100)})
>>> print(df.to_string(max_rows=10))                                          
    one
0     0
1     1
2     2
3     3
4     4
..  ...
95   95
96   96
97   97
98   98
99   99

Same way, you can set max_cols

A slightly less nice (imho) version which returns a slice of the dataframe:

df.iloc[[*range(5), *range(-5, 0)]]

Upvotes: 3

Related Questions