question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add an ends function that shows both head and tail of the df

See original GitHub issue

Add the following function to pd. dataFrame and pd.Series

def ends(df, x=5):
    """Returns both head and tail of the dataframe or series.
    
    Args:
        x (int): Optional number of rows to return for each head and tail
    """
    print('{} rows x {} columns'.format(np.shape(df)[0],np.shape(df)[1]))
    return df.head(x).append(df.tail(x))

Problem description

Often both the beginning and end of a df are of interest, fore example in a time series.

This leads to calling df.head() df.tail() in two seperate notebook cells. This is not only tedious, but also leads to a cluttered notebook. A function that returns both of these + a print on the number of rows and columns, thus allowing a check if the index matches the number of rows.

Example


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

1500 rows x 6 columns

0 1 2 3 4 5
0 0.949695 0.160928 0.434134 0.943103 0.477830 0.903479
1 0.736711 0.103746 0.028694 0.205910 0.226061 0.458452
1498 0.362950 0.586887 0.399681 0.115366 0.239049 0.386281
1499 0.018102 0.852198 0.880993 0.671604 0.705586 0.802237

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None python: 3.6.2.final.0 python-bits: 64 OS: Windows OS-release: 8.1 machine: AMD64 processor: Intel64 Family 6 Model 61 Stepping 4, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.21.0 pytest: 3.3.1 pip: 9.0.1 setuptools: 38.2.4 Cython: 0.26.1 numpy: 1.12.1 scipy: 0.19.1 pyarrow: None xarray: None IPython: 6.1.0 sphinx: 1.6.3 patsy: 0.4.1 dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: 1.2.1 tables: 3.4.2 numexpr: 2.6.2 feather: None matplotlib: 2.0.2 openpyxl: 2.4.8 xlrd: 1.1.0 xlwt: 1.3.0 xlsxwriter: 0.9.8 lxml: 3.8.0 bs4: 4.6.0 html5lib: 0.999999999 sqlalchemy: 1.1.13 pymysql: None psycopg2: None jinja2: 2.10 s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jrebackcommented, Dec 13, 2017

@icfly2 #18749 already is updating he doc-strings, so natural to add there.

Love to have you contribute on other issues!

0reactions
adamrossnelsoncommented, Aug 3, 2021

Worth a mention that this issue also relates to: https://github.com/pandas-dev/pandas/issues/42837

The idea is that there is a way to preserve vertical screen space when inspecting ‘tall’ (many rows, few cols) data frames.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python pandas select both head and tail - Stack Overflow
Small simple function: def ends(df, x=5): return df.head(x).append(df.tail(x)). And use like so: ... You should use both head() and tail() for this purpose....
Read more >
The head() and tail() function in R - Detailed Reference
Head (): Function which returns the first n rows of the dataset. ... Tail(): Function which returns the last n rows of the...
Read more >
pandas.DataFrame.tail — pandas 1.5.2 documentation
This function returns last n rows from the object based on position. It is useful for quickly verifying data, for example, after sorting...
Read more >
How to Select Rows with Head and Tail in Pandas - Softhints
You may use Pandas functions head() and tail() in order to show first and last N rows of a DataFrame simultaneously.
Read more >
Get first/last n rows of DataFrame with head(), tail(), slice
You can use slices to do the same thing as head() and tail() . print(df[:5]) # sepal_length sepal_width petal_length petal_width species ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found