Site icon Console Flare Blog

Pandas String Functions in Python: Full Guide With Examples

Pandas String Functions in Python: Full Guide With Examples

Text data is always messy. You get extra spaces, wrong cases, bad phone numbers, mixed formats, and unclear feedback messages. Cleaning such text becomes easy when you use Pandas string functions. Pandas gives you a large set of tools under the An  .str accessor that helps you edit, format, split, and validate string columns.

This guide shows each important string function with simple examples. Every concept is explained in a clean and natural way so beginners understand without confusion.

You will work on one sample dataset and apply all functions step by step.


Download Sample Dataset: string dataset

1. Load Sample Dataset

import pandas as pd 
df = pd.read_csv("string_dataset.csv") 
df

What this does

You load a CSV file into a DataFrame. Pandas reads the columns automatically.

Where it is used

You need this step in every project before any cleaning.


2. Why Pandas String Functions Are Important

Text comes in many forms. People type with different styles or spellings. Addresses have commas. Names contain extra spaces. Phone numbers include symbols. Feedback messages mix upper and lower case.

Pandas string functions help you do tasks like
• clean whitespace
• convert case
• split text
• remove symbols
• validate numbers
• extract parts of a string
• filter rows by keywords

All string functions work through .str accessor.

Example:

df["FirstName"].str.lower() 
df["Address"].str.split(",")

3. Remove Extra Spaces Using strip() in Pandas String Fucntions

df["FirstName"] = df["FirstName"].str.strip() 
df["LastName"] = df["LastName"].str.strip() 
df["Address"] = df["Address"].str.strip() 
df["Feedback"] = df["Feedback"].str.strip()

What this does

strip removes spaces from the beginning and end of each cell.

Why Using Pandas String Functions Matters

Extra spaces break grouping, merging, and filtering. This is the first cleaning step in almost every project.

Real example

User name entered as " Amit" or " Riya" becomes "Amit" and "Riya".


4. Standardize Text Case

Capitalize the first letter

df["FirstName"] = df["FirstName"].str.capitalize()

Why

It gives a neat format when you display name lists.


Convert last name to uppercase

df["LastName"] = df["LastName"].str.upper()

Why

Customer IDs, last names, or department codes often follow uppercase standards.


Address in lowercase

df["Address"] = df["Address"].str.lower()

Why

Lowercase helps in comparisons and searching.


Change Feedback Case Using Pandas String Functions

df["Feedback"] = df["Feedback"].str.swapcase()

Why

Excellent for examining formatting problems in user input.


5. Find Text Length Using Pandas String Functions (str.len)

Full name length

df["NameLength"] = (df["FirstName"] + df["LastName"]).str.len()

What this does

Counts characters in the combined name.

Where used

Useful in validation and text analysis.


Phone number length

df["PhoneNumber"] = df["PhoneNumber"].astype(str) 
df["PhoneLength"] = df["PhoneNumber"].str.len()

Why

Ten-digit phone numbers are required. A number’s length indicates whether it is valid or not.


6. Identify Invalid Phone Numbers Using Pandas String Functions

Length not 10

df[df["PhoneNumber"].str.len() != 10]

Meaning

Shows numbers that are too short or too long.


Identify Non-Numeric Characters With Pandas str Methods

df[df["PhoneNumber"].str.isnumeric() == False]

Use case

Detect wrong input like
• 98-1234-567
• +91 98123
• “abc12345”


7. Replace Text Using replace()

Replace “street” with “st.”

df["Address"] = df["Address"].str.replace("street", "st.")

Why

Shortens addresses and makes them consistent.


Remove Punctuation From Feedback Using Pandas String Functions

df["Feedback"] = ( 
    df["Feedback"] 
    .str.replace("!", "") 
    .str.replace("?", "") 
    .str.replace(".", "") 
    .str.replace(",", "") 
)

Why

Feedback comments often contain unnecessary punctuation. Removing them helps in sentiment analysis and keyword scanning.


8. For privacy, hide your phone number

df["MaskedPhone"] = df["PhoneNumber"].str.replace(r"\d(?=\d{4})", "*", regex=True)

What it does

Replaces all digits except the last 4 with *.

Example:
9876543210******3210

Where used

on customer display cards, dashboards, and reports.


9. Split Address Column

df[["Area", "City"]] = df["Address"].str.split(",", expand=True)

How this works

divides the address according to the comma into two new columns.

Why it’s useful

separates the city and locality for analysis, grouping, and filtering.


10. Make a city code using the first three letters

df["CityCode"] = df["City"].str[:3]

Meaning

Extracts the first 3 characters.

Example
Mumbai → Mum
Pune → Pun

Where used

naming, tagging, or grouping based on the city.


11. Filter Text Using contains, startswith, endswith

Rows containing refund or damaged

df[df["Feedback"].str.contains("refund|damaged", case=False)]

Use

Find complaints quickly.


Rows containing both not and satisfied

df[ 
   df["Feedback"].str.contains("not", case=False) 
   & df["Feedback"].str.contains("satisfied", case=False) 
]

Use

Detect negative satisfaction comments.


Delivered but not late

df[ 
   df["Feedback"].str.contains("delivered", case=False) 
   & ~df["Feedback"].str.contains("late", case=False) 
]

Use

Find positive delivery comments.


Address starting with park

df[df["Address"].str.startswith("park")]

Use

Group people in a specific area.


Address ending with Mumbai

df[df["Address"].str.endswith("mumbai")]

Use

Fetch city-wise records.


Address containing numbers

df[df["Address"].str.contains(r"\d")]

Use

Find addresses with house numbers.


12. Create Full Name Column

df["FullName"] = df["FirstName"] + " " + df["LastName"]

Why

FullName gives a clean single column useful for reports.


13. Create Advanced Customer Code

df["CustomerCode"] = ( 
    df["FirstName"].str[:2] 
    + "-" 
    + df["CustomerID"].astype(str).str[-2:] 
    + "-" 
    + df["City"].str[::-1] 
)

Breakdown

First 2 letters of name
Last 2 digits of customer ID
Reverse of city name

Example
Aman, ID 1045, “pune” → Am-45-enup

Where used

Unique internal customer IDs.


14. String Indexing

df["AreaCode"] = df["Address"].str[0]

Meaning

Takes the first character of the address.

Use

Quick character-based classification.


15. Slicing Examples

First 3 letters of the first name

df["ShortName"] = df["FirstName"].str[:3]

Reverse last name

df["LastNameReverse"] = df["LastName"].str[::-1]

Reverse the entire address

df["ReverseAddress"] = df["Address"].str[::-1]

Final Note
You work faster when you use Pandas string functions for text cleaning. These tools help you fix cases, remove noise, check formats, and prepare data for analysis. You save time and keep your workflow simple.

Internal link:

  1. Value_counts and Groupby in Pandas Explained in Easy Steps
  2. Date Handling in Pandas in Easy Steps
  3. Aggregate Functions in Pandas: Beginner’s Guide with Examples

Link once to Pandas official documentation:

Pandas Document

Conclusion

Pandas string functions make text cleaning simple. You fix cases, remove noise, check formats, and prepare your data for analysis in less time. These functions help you handle real text problems in datasets, like phone numbers, feedback, comments, and IDs. When you use these tools, your workflow becomes smooth and consistent.

If you want to learn more about data handling, data cleaning, or how Python is used in real projects, join our free workshop on Master in Data Science with Power BI. You will understand how the field works, what skills matter, and why companies offer strong salaries for data roles.

You learn each tool from the basics. This makes you ready for any data-focused profile.

To join, register on ConsoleFlare. You will get a call back.

ConsoleFlare has been listed among the top training institutes. The program teaches you in simple Hindi and follows a clear rule: learn only what is needed for the job.

Want to explore more?
Register yourself on ConsoleFlare.

Console Flare

Exit mobile version