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

# Getting shortest and longest strings in Pandas DataFrame

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

Consider the following Pandas DataFrame:

``` import pandas as pddf = pd.DataFrame({'vals':['aa','bbbb','ccc','dddd','ee']})df.head() vals0 aa1 bbbb2 ccc3 dddd4 ee ```

# Getting the shortest strings in a Pandas column

To get the shortest strings in the `vals` column:

``` s_length = df['vals'].str.len()bool_mask = (s_length == s_length.min())df['vals'][bool_mask] 0 aa4 eeName: vals, dtype: object ```

Here, we first obtain a `Series` holding the length of each string using the `str.len()` method:

``` s_length = df['vals'].str.len()s_length 0 21 42 33 44 2Name: vals, dtype: int64 ```

We then compute the minimum string length using `s_length.min()`, and create a boolean mask where `True` corresponds to strings that are shortest:

``` bool_mask = (s_length == s_length.min())bool_mask 0 True1 False2 False3 False4 TrueName: vals, dtype: bool ```

Finally, we use the `[~]` notation to fetch the values in the `vals` column corresponding to `True`:

``` df['vals'][bool_mask] # Returns a Series 0 aa4 eeName: vals, dtype: object ```

We could also fetch the rows who have the shortest `vals` value as a DataFrame like so:

``` df[bool_mask] # Returns a DataFrame vals0 aa4 ee ```

# Getting the longest strings in a Pandas column

The logic for getting the longest strings is very similar to getting the shortest strings:

``` s_length = df['vals'].str.len()bool_mask = (s_length == s_length.max())df['vals'][bool_mask] # Returns a Series 1 bbbb3 ddddName: vals, dtype: object ```

Here, we use the `max()` instead of `min()` to compute the length of the longest string.

Again, to get the rows whose `vals` string value is longest as a DataFrame instead of a Series:

``` df[bool_mask] # Returns a DataFrame vals1 bbbb3 dddd ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!