0% found this document useful (0 votes)
27 views17 pages

String Manipulation Techniques in Pandas

String manipulation in data wrangling involves operations like changing case, trimming spaces, and replacing substrings to ensure text data is consistent and clean for analysis. In pandas, common methods include .str.lower(), .str.strip(), .str.replace(), and .str.split() among others, which facilitate various text processing tasks. These manipulations are essential for preparing data for further analysis and ensuring the integrity of text data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

String Manipulation Techniques in Pandas

String manipulation in data wrangling involves operations like changing case, trimming spaces, and replacing substrings to ensure text data is consistent and clean for analysis. In pandas, common methods include .str.lower(), .str.strip(), .str.replace(), and .str.split() among others, which facilitate various text processing tasks. These manipulations are essential for preparing data for further analysis and ensuring the integrity of text data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

What is String Manipulation in Data Wrangling?

String manipulation refers to processes such as changing case, trimming spaces, replacing substrings,
extracting patterns, splitting strings, concatenating strings, and more.
These operations help ensure text data is consistent, clean, and structured correctly for downstream
analysis.

Common String Manipulations in pandas


• Changing case: Using .[Link](), .[Link]() to normalize text.
• Trimming whitespace: .[Link](), .[Link](), .[Link]() to clean spaces.
• Replacing text: .[Link]() to substitute parts of strings.
• Splitting and extracting: .[Link]() and .[Link]() for parsing or regex-based extraction.
• Checking patterns: .[Link](), .[Link](), .[Link]().
• Concatenation: .[Link]() to merge or join strings.
• Handling missing values: Many string methods gracefully ignore or handle NaN values.
• Regular expressions: Regex patterns with methods like .[Link](), .[Link]() provide
powerful ways to manipulate complex strings
Example:
import pandas as pd
import numpy as np
data = {'Names': [' Gulshan', 'Shashank', 'Bablu ', 'Abhishek', 'Anand', [Link], 'Pratap']}
df = [Link](data)
print("Original DataFrame:")
print(df)
# Strip spaces
df['Names'] = df['Names'].[Link]()
# Convert to lowercase
df['Names_lower'] = df['Names'].[Link]()
# Replace substring
df['Names_replaced'] = df['Names'].[Link]('a', '@', regex=False)
# Check if name starts with 'b'
df['Starts_with_b'] = df['Names'].[Link]('B')
print("\nAfter String Manipulation:")
print(df)

1
2
String manipulation
.str is an accessor used to apply string methods to each element in a Pandas Series (usually of
dtype=object or string).
Commonly Used String Object Methods in Pandas : Case Handling
Method Description Example
[Link]() Convert to lowercase 'HELLO' → 'hello'
[Link]() Convert to uppercase 'hello' → 'HELLO'
[Link]() Title case 'john doe' → 'John Doe'
[Link]() First letter capitalized 'hello' → 'Hello'
[Link]() Swap upper and lower 'HeLLo' → 'hEllO'

Whitespace and Trimming


Method Description
[Link]() Remove leading/trailing whitespace
[Link]() Remove leading whitespace
[Link]() Remove trailing whitespace

Searching / Matching
Method Description
[Link](pat) Check if substring or pattern exists
[Link](pat) Check if string starts with substring
[Link](pat) Check if string ends with substring
[Link](pat) Regex match at start
[Link](pat) Regex full match
[Link](sub) Index of first occurrence (-1 if not found)
[Link](sub) Last occurrence

Replacing / Splitting / Joining


Method Description
[Link](pat, repl) Replace substrings or patterns
[Link](sep) Split string into list
[Link](sep) Split from right
[Link](sep) Join strings in a list
[Link](n) Get element at position n from list

3
Length and Count
Method Description
[Link]() Length of each string
[Link](sub) Count occurrences of substring

Formatting and Padding


Method Description
[Link](width) Pad string to specified width
[Link](width) Pad with zeros
[Link](width) Center string
[Link](width) Left-justify
[Link](width) Right-justify

Cleaning
Method Description
[Link]() Remove leading/trailing spaces
[Link]() Replace substrings or regex patterns
[Link]() Unicode normalization (with unicodedata)

4
1. split()

import pandas as pd

df = [Link]({'text': ['apple pie', 'banana split', 'cherry tart']})


print("Input:\n", df['text'])

output = df['text'].[Link]()
print("\nOutput of split():\n", output)

Output:

text
0 apple pie
1 banana split
2 cherry tart
Name: text, dtype: object

Output of split():
0 [apple, pie]
1 [banana, split]
2 [cherry, tart]
Name: text, dtype: object

2. endswith()

output = df['text'].[Link]('t')
print("\nOutput of endswith('t'):\n", output)

Output:

text
0 False
1 False
2 True
Name: text, dtype: bool

3. startswith()

output = df['text'].[Link]('b')
print("\nOutput of startswith('b'):\n", output)

Output:

text
0 False
1 True
2 False
Name: text, dtype: bool

5
4. strip()

python
df = [Link]({'text': [' apple ', ' banana ', ' cherry ']})
output = df['text'].[Link]()
print("\nOutput of strip():\n", output)

Output:

text
0 apple
1 banana
2 cherry
Name: text, dtype: object

5. rstrip()

python
df = [Link]({'text': ['apple ', 'banana ', 'cherry ']})
output = df['text'].[Link]()
print("\nOutput of rstrip():\n", output)

Output:

text
0 apple
1 banana
2 cherry
Name: text, dtype: object

6. lstrip()

python
df = [Link]({'text': [' apple', ' banana', ' cherry']})
output = df['text'].[Link]()
print("\nOutput of lstrip():\n", output)

Output:

text
0 apple
1 banana
2 cherry
Name: text, dtype: object

6
7. concat()

python
df = [Link]({'text': ['apple', 'banana', 'cherry']})
output = df['text'].[Link](sep=', ')
print("\nOutput of concat():\n", output)

Output:

text
apple, banana, cherry

8. join()

python
series = [Link]([['apple', 'pie'], ['banana', 'split'], ['cherry', 'tart']])
output = [Link]('_')
print("\nOutput of join('_'):\n", output)

Output:

text
0 apple_pie
1 banana_split
2 cherry_tart
dtype: object

9. index()

python
df = [Link]({'text': ['apple', 'banana', 'cherry']})
output = df['text'].[Link]('a') # Note: raises error if 'a' not found
print("\nOutput of index('a'):\n", output)

Output:

text
0 0
1 1
2 2
Name: text, dtype: int64

10. find()

python
output = df['text'].[Link]('a') # returns -1 if not found
print("\nOutput of find('a'):\n", output)

7
Output:

text
0 0
1 1
2 2
Name: text, dtype: int64

11. rfind()

python
output = df['text'].[Link]('a') # last occurrence of 'a'
print("\nOutput of rfind('a'):\n", output)

Output:

text
0 0
1 3
2 -1
Name: text, dtype: int64

12. count()

python
output = df['text'].[Link]('a')
print("\nOutput of count('a'):\n", output)

Output:

text
0 1
1 3
2 0
Name: text, dtype: int64

13. replace()

python
output = df['text'].[Link]('a', '@')
print("\nOutput of replace('a', '@'):\n", output)

Output:

text
0 apple
1 b@n@n@

8
2 cherry
Name: text, dtype: object

14. lower()

python
df = [Link]({'text': ['APPLE', 'BANANA', 'CHERRY']})
output = df['text'].[Link]()
print("\nOutput of lower():\n", output)

Output:

text
0 apple
1 banana
2 cherry
Name: text, dtype: object

15. upper()

python
df = [Link]({'text': ['apple', 'banana', 'cherry']})
output = df['text'].[Link]()
print("\nOutput of upper():\n", output)

Output:

text
0 APPLE
1 BANANA
2 CHERRY
Name: text, dtype: object

16. rjust()

python
df = [Link]({'text': ['apple', 'banana', 'cherry']})
output = df['text'].[Link](10, fillchar='-')
print("\nOutput of rjust(10, '-')):\n", output)

Output:

text
0 -----apple
1 ----banana
2 ----cherry
Name: text, dtype: object

9
17. ljust()

python
output = df['text'].[Link](10, fillchar='-')
print("\nOutput of ljust(10, '-')):\n", output)

Output:

text
0 apple-----
1 banana----
2 cherry----
Name: text, dtype: object

10
String function using series object
Function Description Syntax Example
Name
lower() Converts all characters to lowercase [Link]()
upper() Converts all characters to uppercase [Link]()
strip() Removes leading and trailing whitespace or specified [Link]()
characters
lstrip() Removes leading whitespace or specified characters [Link]()
rstrip() Removes trailing whitespace or specified characters [Link]()
split() Splits strings by delimiter into lists or expands columns [Link]('_')
contains() Checks if string contains a substring or regex pattern, returns [Link](
boolean Series 'pattern')
replace() Replaces occurrences of a pattern or string [Link]('
old', 'new')
startswith() Checks if strings start with specified prefix [Link]
h('prefix')
endswith() Checks if strings end with specified suffix [Link]
('suffix')
len() Returns length of each string [Link]()
isdigit() Returns True for strings composed only of digits [Link]()
isalpha() Returns True for strings composed only of alphabetic [Link]()
characters
extract() Extracts capture groups in regex as DataFrame columns [Link](r'
(pattern)')
cat() Concatenates strings with specified separator [Link](sep='
, ')
get_dummies() Splits strings by separator and returns indicator variables `[Link].get_du
mmies(sep='
removeprefix() Removes prefix from strings [Link]
refix('pre')
removesuffix() Removes suffix from strings [Link]
uffix('suf')

11
12
Function Description Example Syntax Input Example Output Example

strip() Remove df['Name'].[Link]() [' Alice Smith ', ['Alice Smith',


leading/trailing 'Bob Jones', 'Bob Jones',
spaces
'Charlie Brown', 'Charlie Brown',
'Dana White'] 'Dana White']

lstrip() Remove leading df['Name'].[Link]() [' Alice Smith ', ['Alice Smith ', 'Bob
spaces 'Bob Jones', Jones', 'Charlie Brown',
'Dana White']
'Charlie Brown', 'Dana
White']

rstrip() Remove trailing df['Name'].[Link]() [' Alice Smith ', [' Alice Smith',
spaces
'Bob Jones', 'Bob Jones',
'Charlie Brown', 'Charlie Brown',
'Dana White'] 'Dana White']

split() Split string into list df['Name'].[Link]().[Link]() [' Alice Smith ', [['Alice', 'Smith'],
of words
'Bob Jones', ['Bob', 'Jones'],
'Charlie Brown', 'Dana ['Charlie', 'Brown'],
White'] ['Dana', 'White']]

startswith() Check if string df['Name'].[Link]().[Link]('A') [' Alice Smith ', [True,


starts with 'A'
'Bob Jones', False,
'Charlie Brown', False,
'Dana White'] False]

endswith() Check if string df['Name'].[Link]().[Link]('e') [' Alice Smith ', [False,


ends with 'e' 'Bob Jones', False,
'Charlie Brown', False,
'Dana White'] True]

lower() Convert to df['Message'].[Link]() ['Hello world', ['hello world',


lowercase 'Welcome to pandas', 'welcome to pandas',
'hello again', 'hello again',
'HELLO'] 'hello']

upper() Convert to df['Message'].[Link]() ['Hello world', ['HELLO WORLD',


uppercase 'Welcome to pandas', 'WELCOME TO
'hello again', PANDAS',
'HELLO'] 'HELLO AGAIN',
'HELLO']

replace() Replace 'world' df['Message'].[Link]('world', 'earth') ['Hello world', ['Hello earth',


with 'earth' 'Welcome to pandas', 'Welcome to pandas',
'hello again', 'hello again',
'HELLO'] 'HELLO']

count() Count occurrences df['Message'].[Link]('l') ['Hello world', [3,


of 'l' 'Welcome to pandas', 2,
'hello again', '
HELLO'] 2,
2]

find() Find first index of df['Message'].[Link]('o') ['Hello world', [4,


'o' 'Welcome to pandas', 4,
'hello again', 1,
'HELLO'] -1]

rfind() Find last index of df['Message'].[Link]('o') ['Hello world', [7,


'o' 'Welcome to pandas', 4,
'hello again', 'HELLO']
1,
-1]

join() Join characters with df['Name'].[Link]().[Link](' ', [' Alice Smith ', ['A-l-i-c-e-S-m-i-t-h',
'').[Link]('-')
'-' 'Bob Jones', 'B-o-b-J-o-n-e-s',
'Charlie Brown', 'C-h-a-r-l-i-e-B-r-o-w-n',
'Dana White'] 'D-a-n-a-W-h-i-t-e']

concat (+) Concatenate Name df['Name'].[Link]() + ' - ' + [' Alice Smith , Hello ['Alice Smith - Hello
df['Message']
and Message world', world',
'Bob Jones, Welcome 'Bob Jones - Welcome to
to pandas', pandas',
'Charlie Brown, hello 'Charlie Brown - hello
again', again',
'Dana White, HELLO'] 'Dana White - HELLO']

index (get) Get first word of df['Name'].[Link]().[Link]().[Link](0) [' Alice Smith ', ['Alice',
Name 'Bob Jones', 'Bob',
'Charlie Brown', 'Charlie',
'Dana White'] 'Dana']

rjust() Pad left to align df['Name'].[Link]().[Link](15, '*') [' Alice Smith ', ['**Alice Smith',
right 'Bob Jones', '*****Bob Jones',
'Charlie Brown',
'Charlie Brown',
'***Dana White']
'Dana White']

ljust() Pad right to align df['Name'].[Link]().[Link](15, '#') [' Alice Smith ', ['Alice Smith##',
left 'Bob Jones', 'Bob Jones#####',
'Charlie Brown', 'Charlie Brown##',

'Dana White'] 'Dana White###']

You might also like