About this question
How to find find the length of dataframe string stored in each cell. What code should I use to do this?
How to find find the length of dataframe string stored in each cell. What code should I use to do this?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on Apr 14, 2021
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.
You can find the length of dataframe using the below code:
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
Ranjana Admin JanBask Expert Latest answer
Answered on Mar 17, 2025
If you’re working with Pandas in Python and need to find the length of strings in a DataFrame column, here’s how you can do it:
1. Using .str.len() to Get String Length
import pandas as pd
# Create a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David']}
df = pd.DataFrame(data)
# Add a new column with string lengths
df['Name_Length'] = df['Name'].str.len()
print(df)This will output:
Name Name_Length
0 Alice 5
1 Bob 3
2 Charlie 7
3 David 52. Handling Missing or Non-String Values
If your DataFrame contains NaN values, you should handle them properly:
df['Name_Length'] = df['Name'].astype(str).str.len() This ensures NaN values don’t cause errors.
3. Applying to Multiple Columns
If you want to apply .str.len() to multiple columns:
df[['Col1_Length', 'Col2_Length']] = df[['Col1', 'Col2']].astype(str).apply(lambda x: x.str.len())4. Final Thoughts
Ranjana Admin JanBask Expert
Answered on May 2, 2024
To find the length of strings in a DataFrame column using Python Pandas, you can use the str.len() method. Here's how you can do it:
import pandas as pd
# Create a sample DataFrame
data = {'Name': ['John', 'Alice', 'Bob', 'Charlie'],
'City': ['New York', 'Los Angeles', 'Chicago', 'Boston']}
df = pd.DataFrame(data)
# Add a new column with the length of strings in the 'Name' column
df['Name_Length'] = df['Name'].str.len()
# Display the DataFrame
print(df)Output:
Name City Name_Length
0 John New York 4
1 Alice Los Angeles 5
2 Bob Chicago 3
3 Charlie Boston 7In this example, we first create a DataFrame with columns 'Name' and 'City'. Then, we use the str.len() method to compute the length of strings in the 'Name' column and assign the result to a new column 'Name_Length'. Finally, we print the DataFrame to see the result.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…