0
0
Data Analysis Pythondata~5 mins

String accessor (.str) methods in Data Analysis Python

Choose your learning style9 modes available
Introduction

We use string accessor methods to easily work with text data inside tables. They help us clean, search, and change text in columns.

You have a column of names and want to make them all lowercase.
You want to find rows where a text column contains 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 replace parts of text in a column.
Syntax
Data Analysis Python
df['column_name'].str.method_name(arguments)

The .str lets you use string methods on each item in a column.

It works only on columns with text (strings).

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

This code shows how to clean text by removing spaces and changing case, find emails with 'gmail', and split full names into parts.

Data Analysis Python
import pandas as pd

data = {'name': ['Alice ', ' Bob', 'CHARLIE'],
        'email': ['alice@example.com', 'bob@gmail.com', 'charlie@yahoo.com'],
        'full_name': ['Alice Smith', 'Bob Brown', 'Charlie Black']}

df = pd.DataFrame(data)

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

# Find emails containing 'gmail'
gmail_filter = df['email'].str.contains('gmail')

# Split full names into first and last
split_names = df['full_name'].str.split(' ')

print('Clean Names:')
print(clean_names)
print('\nEmails with gmail:')
print(gmail_filter)
print('\nSplit Full Names:')
print(split_names)
OutputSuccess
Important Notes

String accessor methods work only on columns with string data. If there are missing or non-string values, you might get errors.

You can chain multiple .str methods to do several text operations in one line.

Common methods include lower(), upper(), strip(), contains(), replace(), and split().

Summary

.str lets you use string methods on each item in a text column.

It helps clean, search, and change text data easily.

You can chain methods to do multiple text operations step-by-step.