In this short guide, I'll show you how to count the number of characters in a string column and store the result as a new column in a Pandas DataFrame.
(1) Quick and Fast Solution Using .str.len()
df['char_count'] = df['text'].str.len()
(2) Using apply(len)
as an Alternative
df['char_count'] = df['text'].apply(len)
This is useful when analyzing text data, measuring string lengths, or filtering based on character count. For example when you need to filter out some outliers or analyse data based on the groups.
1: Example DataFrame with Text Column
Let's create a sample DataFrame with a column containing text:
import pandas as pd
# Sample data
data = {
'text': ['Hello', 'Pandas', 'DataFrame', 'Python is fun!']
}
df = pd.DataFrame(data)
df
Output:
text | |
---|---|
0 | Hello |
1 | Pandas |
2 | DataFrame |
3 | Python is fun! |
2: Count Characters in Each String
The easiest way to count characters in a string column is using .str.len()
:
df['char_count'] = df['text'].str.len()
Output:
text | char_count | |
---|---|---|
0 | Hello | 5 |
1 | Pandas | 6 |
2 | DataFrame | 10 |
3 | Python is fun! | 14 |
3: Alternative Method Using apply(len)
Another way to count characters is by using apply(len)
, which applies Python’s built-in len()
function to each row:
df['char_count'] = df['text'].apply(len)
This method is slightly slower than .str.len()
but works well in most cases.
Conclusion
In this guide, we learned how to:
- Count the number of characters in a string column
- Create a new column to store the character count
- Use
.str.len()
for better performance - Use
apply(len)
as an alternative