search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | truncate method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
tocTable of Contents
expand_more
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Panda's DataFrame.truncate(~) method extracts a subset of rows from the DataFrame using truncation.

Parameters

1. before | int or string or date | optional

Remove all values before this index value. Leaving this out would mean that rows/columns will be included from the beginning.

2. after | int or string or date | optional

Remove all values after this index value. Leaving this out would mean that rows/columns will be included until the end.

3. axis | int or string | optional

The axis along which to perform the method:

Axis

Description

0 or "index"

Truncate rows

1 or "columns"

Truncate columns

By default, axis=0.

4. copy | bool | optional

  • If True, then a new DataFrame will be created.

  • If False, then a reference to the source DataFrame is returned. This means that modifying the DataFrame returned by truncate(~) would also modify the source DataFrame, and vice versa.

By default, copy=True.

Examples

Truncating rows

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2,3,4], "B":[5,6,7,8]}, ["a","b","c","d"])
df
A B
a 1 5
b 2 6
c 3 7
d 4 8

To truncate rows before index "b" and after index "c":

df.truncate(before="b", after="c") # or axis=0
A B
b 2 6
c 3 7

Truncating columns

Consider the following DataFrame:

df = pd.DataFrame({"A":[1,2], "B":[3,4], "C":[5,6], "D":[7,8]})
df
A B C D
0 1 3 5 7
1 2 4 6 8

To truncate columns before column "B" and after column "C", set axis=1:

df.truncate(before="B", after="C", axis=1)
B C
0 3 5
1 4 6
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!