search
Search
Unlock 100+ guides
search toc
close
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
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | trim_zeros method

schedule Aug 12, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Numpy's `trim_zeros()` method returns a new Numpy array with the leading (front) and trailing (back) zeros removed.

# Parameters

1. `a` | `array-like`

An one-dimensional array.

2. `trim`link | `string` | `optional`

Whether to trim only the leading, trailing or both leading and trailing zeros:

Value

Meaning

"f"

Trims the front zeros.

"b"

Trims the zeros at the back.

"fb"

Trim both the front and the back zeros.

By default, `trim="fb"`.

# Return value

A new Numpy array with the leading and trailing zeros removed.

# Examples

## Trimming both leading and trailing zeros

``` a = np.array([0,4,5,0,0])np.trim_zeros(a) array([4, 5]) ```

## Trimming only the leading zeros

``` a = np.array([0,4,5,0,0])np.trim_zeros(a, trim="f") array([4, 5, 0, 0]) ```

## Trimming only trailing zeros

``` a = np.array([0,4,5,0,0])np.trim_zeros(a, trim="b") array([0, 4, 5]) ```
Edited by 0 others
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!