**Pandas**

655 guides

*chevron_left*

**Series Cookbook**

Appending values to a SeriesApplying a function to SeriesBinning values in a SeriesChanging data type of SeriesChecking if a value is NaN in Pandas SeriesChecking if all values are NaN in SeriesChecking if all values in a Series are uniqueChecking if Series has missing valuesConverting Python list to SeriesConverting Series of lists into DataFrameConverting Series to a Numpy arrayConverting Series to Python listCounting frequency of values in SeriesCreating a Series of zeroesCreating a Series with constant valueFiltering strings based on length in SeriesFiltering values of a SeriesGetting frequency counts of values in intervalsGetting index of largest valueGetting index of smallest valueGetting index of value in SeriesGetting integer index of largest valueGetting integer index of smallest valueGetting integer index of value in SeriesGetting intersection of SeriesGetting length of each string in SeriesGetting list of integer indices where value is boolean True in SeriesGetting the index of the nth value in SeriesGetting the most frequent value in SeriesGetting value of Series using integer indexGrouping Series by its valuesHandling error - "Truth value of a Series is ambiguous"Inverting a Series of booleansRemoving missing values from a SeriesRemoving substrings from strings in a SeriesRemoving values from SeriesResetting index of SeriesSorting values in a SeriesSplitting strings based on spaceStripping leading and trailing whitespaceTaking the floor or ceiling of values in SeriesUsing index.get_loc(~) for multiple values

check_circle

Mark as learned thumb_up

0

thumb_down

0

chat_bubble_outline

0

Comment auto_stories Bi-column layout

settings

# Filtering strings based on length in Pandas Series

*schedule*Aug 10, 2023

local_offer

Tags Python●Pandas

*toc*Table of Contents

*expand_more*

Master the

Start your free 7-days trial now!

**mathematics behind data science**with 100+ top-tier guidesStart your free 7-days trial now!

# Solution

To filter strings based on their length in Pandas Series:

```
s = pd.Series(["aaa","aa","bbb"])s.loc[s.str.len() == 3]
0 aaa2 bbbdtype: object
```

Here, we are preserving strings that are of length `3`

.

# Explanation

We first fetch the length of each string using `str.len()`

:

```
s.str.len()
0 31 22 3dtype: int64
```

We then get a boolean mask where `True`

indicates an entry that is of length `3`

:

```
s.str.len() == 3
0 True1 False2 Truedtype: bool
```

Finally, we pass in this boolean mask into `loc`

to fetch the entries in `s`

corresponding to `True`

:

```
s.loc[s.str.len() == 3]
0 aaa2 bbbdtype: object
```

# Related

Pandas Series str | len method

Returns the length of each string in the Series.

*chevron_right*

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!