0
0
Data Analysis Pythondata~5 mins

String methods on Series in Data Analysis Python

Choose your learning style9 modes available
Introduction

String methods on Series help you work with text data easily. They let you change, search, or check text in many rows at once.

You have a list of names and want to make them all lowercase.
You want to find which rows contain a certain word.
You need to remove extra spaces from text in a column.
You want to count how many times a letter appears in each text.
You want to replace a part of the text in many rows.
Syntax
Data Analysis Python
series.str.method_name(arguments)
Use .str to access string methods on a pandas Series.
These methods work on each element (row) in the Series.
Examples
Convert all text in the 'name' column to lowercase.
Data Analysis Python
df['name'].str.lower()
Check which rows in 'name' contain the word 'john'. Returns True or False for each row.
Data Analysis Python
df['name'].str.contains('john', na=False)
Remove spaces at the start and end of each text in 'name'.
Data Analysis Python
df['name'].str.strip()
Replace all 'a' letters with '@' in the 'name' column.
Data Analysis Python
df['name'].str.replace('a', '@')
Sample Program

This code shows how to clean text by removing spaces, convert to lowercase, check for a letter, and replace letters in a pandas Series.

Data Analysis Python
import pandas as pd

data = {'name': [' Alice ', 'Bob', 'Charlie', 'David', 'Eve']}
df = pd.DataFrame(data)

# Remove spaces
clean_names = df['name'].str.strip()

# Convert to lowercase
lower_names = clean_names.str.lower()

# Check if name contains letter 'a'
contains_a = lower_names.str.contains('a', na=False)

# Replace 'a' with '@'
replaced_names = lower_names.str.replace('a', '@')

print('Original names:')
print(df['name'])
print('\nCleaned names:')
print(clean_names)
print('\nLowercase names:')
print(lower_names)
print('\nContains letter a:')
print(contains_a)
print('\nNames with a replaced by @:')
print(replaced_names)
OutputSuccess
Important Notes

String methods on Series do not change the original data unless you assign the result back.

Use na=False in methods like contains() to handle missing values safely.

Many string methods are similar to Python's string methods but work on whole columns.

Summary

Use .str to apply string methods on pandas Series.

These methods help clean, search, and modify text data easily.

They work on each row and return a new Series with the results.