search
Search
Unlock 100+ guides
search toc
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
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 the longest string in a column in Pandas DataFrame

schedule May 20, 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 DataFrame:

``` import pandas as pddf = pd.DataFrame({"A":["a","abc","def"]})df A0 a1 abc2 def ```

# Solution

To get the longest string in column `A`:

``` import numpy as nplengths = df["A"].str.len()argmax = np.where(lengths == lengths.max())df.iloc[argmax] A1 abc2 def ```

Here, the returned value is a `DataFrame`. If you wanted a list of the longest strings instead:

``` df["A"].iloc[argmax].to_numpy().ravel().tolist() ['abc', 'def'] ```

Note the following:

# Explanation

We first start by computing the length of each string in column `A` using the Series' `str.len()` method:

``` lengths = df["A"].str.len() # returns a Serieslengths 0 11 32 3Name: A, dtype: int64 ```

We then get a Series of booleans where `True` indicates the position of the longest strings:

``` lengths == lengths.max() 0 False1 True2 TrueName: A, dtype: bool ```

We then use NumPy's `where(~)` to get all the integer indexes of `True`:

``` argmax = np.where(lengths == lengths.max())argmax array([1, 2]) ```

The `` is needed at the back because `where(~)` returns a tuple where the first element is the integer indexes.

Finally, we use the `iloc` property to extract the rows in `df` given these integer indexes:

``` df.iloc[argmax] A1 abc2 def ```

# Why argmax method does not work

You may be tempted to directly use the Series' `argmax(~)` method like so:

``` df.iloc[df["A"].str.loc().argmax()] A abcName: 1, dtype: object ```

However, the problem with `argmax(~)` is that it only returns the integer index of the first occurrence of the maximum, as demonstrated in the output above.

thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!