search
Search
Join our weekly DS/ML newsletter layers DS/ML Guides
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
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
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | truncate method

Programming
chevron_right
Python
chevron_right
Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Data Selection and Renaming
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags
tocTable of Contents
expand_more

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

Truncate rows

0 or "index"

Truncate columns

1 or "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
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!