0
0
Pandasdata~5 mins

str accessor for string methods in Pandas

Choose your learning style9 modes available
Introduction

We use the str accessor to easily work with text data inside pandas columns. It helps us change, check, or clean strings in a simple way.

You have a column of names and want to make them all lowercase.
You want to check which rows contain a certain word.
You need to remove extra spaces from text data.
You want to split full names into first and last names.
You want to count how many times a letter appears in each string.
Syntax
Pandas
DataFrame['column_name'].str.method_name(arguments)

The str accessor works only on columns with text (strings).

You can chain many string methods using .str like .str.lower(), .str.contains(), etc.

Examples
Converts all names in the 'name' column to lowercase.
Pandas
df['name'].str.lower()
Checks which emails contain '@gmail.com' and returns True or False.
Pandas
df['email'].str.contains('@gmail.com')
Removes spaces at the start and end of each address.
Pandas
df['address'].str.strip()
Splits full names into parts by spaces.
Pandas
df['full_name'].str.split(' ')
Sample Program

This code creates a small table with names and emails. It cleans the names by removing spaces and making them lowercase. Then it checks which emails are from Gmail.

Pandas
import pandas as pd

data = {'name': ['Alice ', 'BOB', 'Charlie', 'dave'],
        'email': ['alice@example.com', 'bob@gmail.com', 'charlie@yahoo.com', 'dave@gmail.com']}
df = pd.DataFrame(data)

# Clean names by stripping spaces and making lowercase
df['clean_name'] = df['name'].str.strip().str.lower()

# Check which emails are from gmail
df['is_gmail'] = df['email'].str.contains('@gmail.com')

print(df)
OutputSuccess
Important Notes

If the column has missing values (NaN), string methods will skip them safely.

Not all Python string methods are available, but pandas covers most common ones.

Summary

The str accessor lets you use string methods on pandas columns easily.

It helps clean, check, and change text data in tables.

Use it whenever you work with text inside pandas DataFrames.