Pandas DataFrame truncate() method with examples

Spread the love

The truncate() method in pandas truncate a Series or DataFrame before and after some index value. This is a useful shorthand for boolean indexing based on index values above or below certain thresholds.

Syntax –

DataFrame.truncate(before=None, after=None, axis=None, copy=True)

before – date, str, int – Truncate all rows before this index value.

after – date, str, int – Truncate all rows after this index value.

axis – {0 or ‘index’, 1 or ‘columns’}, – Axis to truncate. Truncates the index (rows) by default.

copy – bool, default is True – Return a copy of the truncated section.

Examples –

Let’s create a dataframe to work with.

import pandas as pd

data = {'A':[89, 89, 90, 110, 125, 84, 131, 123, 123, 140, 145, 145],
       'B': [46, 46, 50, 65, 63, 48, 110, 120, 60, 42, 47, 62],
       'C': [26, 30, 30, 25, 38, 22, 22, 36, 20, 27, 23, 34 ],
       'D': [80, 80, 90, 125, 130, 150, 140, 140, 135, 135, 80, 90]}

df = pd.DataFrame(data)
df

1 . Truncate Rows –

By default, pandas truncate rows. Let’s say that we only want to keep the rows between 5th row and 8th row and truncate all rows before the 5th row and all the rows after the 8th row. We can do this using the truncate method in pandas. Remember indexing in python starts with 0.

df.truncate(before=4, after=7)

2 . Truncate Columns –

We can also truncate columns using truncate method. For this we need to set the axis parameter to axis=1 or columns. Let’s say we want to only keep the middle two columns and truncate all the columns before B and all the columns after C.

df.truncate(before="B", after="C", axis='columns')

3 . Truncate Datatime –

We can also truncate datetime index using truncate method. Let’s create a dataframe which contains datetime.

dates = pd.date_range('2022-01-01', '2022-02-01', freq='s')
df = pd.DataFrame(index=dates, data={'A': 1})
df.tail()
df.truncate(before=pd.Timestamp('2022-01-05'),
            after=pd.Timestamp('2022-01-10')).tail()

Because the index is a DatetimeIndex containing only dates, we can specify before and after as strings.

df.truncate('2022-01-05', '2022-01-10').tail()

Rating: 1 out of 5.

Leave a Reply