Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

Hi. I want to find the length of the string stored in each cell of the dataframe. What code should I use to do this?

1 Answer

0 votes
by (25.1k points)
edited by

To find the length of strings in a data frame you have the len method on the dataframes str property. But to do this you need to call this method on the column that contains the string data.

For example:

import pandas as pd

data = pd.DataFrame({

    'age' : [15, 17, 20, 14, 25],

    'name': ["Sample", "New User", "My Name", "Jane Doe", "John Doe"]

})

data['name'].str.len()

You'll get the following output:

0    6

1    8

2    7

3    8

4    8

Name: name, dtype: int64

You can learn more about pandas and other data science libraries by watching this video:

Related questions

0 votes
1 answer
asked Oct 30, 2020 in Python by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Apr 28, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer

Browse Categories

...