> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Count Characters in a Column and Create a new Length Column in Pandas
- URL: https://datascientyst.com/count-characters-in-a-column-and-create-a-new-length-column-in-pandas/
- Published: 2025-02-17T21:30:17.000Z
- Updated: 2025-02-17T22:01:40.000Z
- Author: John D K
- Tags: Column

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()`**

```python
df['char_count'] = df['text'].str.len()

```

**(2) Using `apply(len)` as an Alternative**

```python
df['char_count'] = df['text'].apply(len)

```

![](https://datascientyst.com/content/images/2025/02/count-characters-in-a-column-and-create-a-new-column-in-pandas.webp)

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:

```python
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()`:

```python
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:

```python
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

## **Resources**

- [Pandas .str.len() Documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.len.html?ref=datascientyst.com)
- [Pandas .apply() Function](https://pandas.pydata.org/docs/reference/api/pandas.Series.apply.html?ref=datascientyst.com)
- [Python len() Function](https://docs.python.org/3/library/functions.html?ref=datascientyst.com#len)