MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Vectorized String Methods (.str accessor)0%
Study MaterialsCheatsheet
PandasProgress 0%
1.1What is Pandas & Core Architecture1.2Pandas Installation & Environment Setup1.3Creating Series & DataFrames from Scratch
5.1Applying Custom Functions (apply, map, applymap)5.2Vectorized String Methods (.str accessor)5.3Renaming Columns, Replacing Values & Sorting
HomeTutorialsPandasVectorized String Methods (.str accessor)
Renaming Columns, Replacing Values & Sorting

Vectorized String Methods (.str accessor)

Intermediate6 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

1. The Power of the .str Accessor

When working with textual data in Pandas, you do not need to write loops or apply() calls to clean strings. Pandas provides the specialized .str accessor, allowing you to execute string operations across entire columns simultaneously with built-in null-safety (automatically skipping NaN values without crashing).


2. Common Text Cleaning Operations

1import pandas as pd
2 
3df = pd.DataFrame({
4 'Raw_Name': [' amit sharma ', 'PRIYA VERMA', 'rohan kumar ', None],
5 'Email': ['amit.s@gmail.com', 'priya.v@yahoo.com', 'rohan.k@outlook.com', None],
6 'Phone': ['+91-98765-43210', '+91-87654-32109', '+91-76543-21098', None]
7})
8 
9# 1. Stripping whitespace & changing case
10df['Clean_Name'] = df['Raw_Name'].str.strip().str.title()
11 
12# 2. String replacement
13df['Clean_Phone'] = df['Phone'].str.replace('+', '').str.replace('-', '')
14 
15# 3. String contains (pattern matching)
16is_gmail = df['Email'].str.contains('gmail', case=False, na=False)
17print("Gmail Users:\n", df[is_gmail])

3. Splitting Strings into Multiple Columns (expand=True)

When a column contains concatenated data (e.g. First_Name Last_Name or City, State):

1# Split Name into First and Last names
2split_names = df['Clean_Name'].str.split(' ', expand=True)
3split_names.columns = ['First_Name', 'Last_Name']
4print(split_names)

Output:

1 First_Name Last_Name
20 Amit Sharma
31 Priya Verma
42 Rohan Kumar
53 NaN NaN

4. Regular Expression (Regex) Extraction (.str.extract)

Extract specific patterns (such as email domains or pincodes) using regex capture groups:

1# Extract email domain using regex capture group
2df['Domain'] = df['Email'].str.extract(r'@([\w\.]+)')
3print(df[['Email', 'Domain']])

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which accessor prefix is used to call vectorized string operations on a Pandas Series?
Question 2
What does setting expand=True do in Series.str.split(',', expand=True)?
Question 3
What does the na=False argument achieve in Series.str.contains('keyword', na=False)?
Question 4
Which method strips whitespace from both the beginning and end of every string in a Series?
Question 5
Which method uses regex capture groups (...) to parse structured tokens into new DataFrame columns?

Next Lesson

Renaming Columns, Replacing Values & Sorting

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Applying Custom Functions (apply, map, applymap)Renaming Columns, Replacing Values & Sorting

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

12-Month Data Analysis Mastery Program

Master Full-Stack Data Analytics (Excel, SQL, Python, Power BI & Tableau)

Combine 15 complete courses, 150 chapters, 541 topics, live SQL/Power BI/Tableau dashboards, and Scikit-Learn capstones at MSK Institute Shikohabad or Live Online.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext