0% found this document useful (0 votes)
15 views1 page

Text Data Manipulation in Python

The document provides examples of using string methods in pandas to manipulate and extract information from text data. It shows how to format, detect patterns, extract matches, split, modify case, pad and join strings using methods like .str.contains(), .str.split(), .str.lower(), .str.pad(), and others. The examples use pandas Series containing string data about suits of cards and rock/paper/scissors to demonstrate the various string manipulation techniques.

Uploaded by

Clóvis Nóbrega
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)
15 views1 page

Text Data Manipulation in Python

The document provides examples of using string methods in pandas to manipulate and extract information from text data. It shows how to format, detect patterns, extract matches, split, modify case, pad and join strings using methods like .str.contains(), .str.split(), .str.lower(), .str.pad(), and others. The examples use pandas Series containing string data about suits of cards and rock/paper/scissors to demonstrate the various string manipulation techniques.

Uploaded by

Clóvis Nóbrega
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

Working with text
 > Formatting settings > Detecting Matches

data in Python # Generate an example DataFramed named df

df = [Link]({"x": [0.123, 4.567, 8.901]})

# Detect if a regex pattern is present in strings with .[Link]()

[Link]("[ae]") # False True True True

# x
# Count the number of matches with .[Link]()

# 0 0.123
[Link]("[ae]") # 0 1 2 2

# 1 4.567

Learn Python online at [Link] # 2 8.901 # Locate the position of substrings with [Link]()

[Link]("e") # -1 -1 1 4

# Visualize and format table output

[Link](precision = 1)

- x The output of [Link]


> Extracting matches
Example data used throughout 0 0.1 is an HTML table
>
this cheat sheet 1 4.5 # Extract matches from strings with [Link]()

[Link](".[ae]") # [] ["ia"] ["he"[ ["pa", "de"]

2 8.9

Throughout this cheat sheet, we’ll be using two pandas series named suits and # Extract capture groups with .[Link]()

rock_paper_scissors. [Link]("([ae])(.)")

# 0 1

import pandas as pd

Splitting strings
# match

suits = [Link](["clubs", "Diamonds", "hearts", "Spades"])

> # 1 0
# 2 0
a m

e a

rock_paper_scissors = [Link](["rock ", " paper", "scissors"]) # 3 0 a d

# Split strings into list of characters with .[Link](pat="")


# 1 e s

[Link](pat="")


# Get subset of strings that match with x[[Link]()]

String lengths and substrings


# [, "c" "l" "u" "b" "s", ]
suits[[Link]("d")] # "Diamonds" "Spades"

> # [, "D" "i" "a" "m" "o" "n" "d" "s", ]

# [, "h" "e" "a" "r" "t" "s", ]

# [, "S" "p" "a" "d" "e" "s", ]

# Get the number of characters with .[Link]()

[Link]() # Returns 5 8 6 6

# Split strings by a separator with .[Link]()

[Link](pat = "a")


> Replacing matches


# Get substrings by position with .str[]

# Replace a regex match with another string with .[Link]()

[Link][2:5] # Returns "ubs" "amo" "art" "ade"

# ["clubs"]

[Link]("a", "4") # "clubs" "Di4monds" "he4rts" "Sp4des"

# ["Di", "monds"]

# Get substrings by negative position with .str[]


# ["he", "rts"]

# Remove a suffix with .[Link]()

[Link][:-3] # "cl" "Diamo" "hea" "Spa

# ["Sp", "des"]

[Link] # "club" "Diamond" "heart" "Spade"

# Remove whitespace from the start/end with .[Link]()


# Split strings and return DataFrame with .[Link](expand=True)
# Replace a substring with .str.slice_replace()

rock_paper_scissors.[Link]() # "rock" "paper" "scissors"

[Link](pat = "a", expand=True)



rhymes = [Link](["vein", "gain", "deign"])

[Link].slice_replace(0, 1, "r") # "rein" "rain" "reign"

# Pad strings to a given length with .[Link]()


# 0 1

[Link](8, fillchar="_") # "___clubs" "Diamonds" "__hearts" "__Spades" # 0 clubs None

# 1 Di monds

# 2 he rts

# 3 Sp des

> Changing case


# Convert to lowercase with .[Link]()
> Joining or concatenating strings Learn Python Online at
[Link]() # "clubs" "diamonds" "hearts" "spades"

[Link]
# Convert to uppercase with .[Link]()
# Combine two strings with +

[Link]() # "CLUBS" "DIAMONDS" "HEARTS" "SPADES"

suits + "5" # "clubs5" "Diamonds5" "hearts5" "Spades5"

# Convert to title case with .[Link]()


# Collapse character vector to string with .[Link]()

[Link]("hello, world!").[Link]() # "Hello, World!"

[Link](sep=", ") # "clubs, Diamonds, hearts, Spades"

# Convert to sentence case with .[Link]()


# Duplicate and concatenate strings with *

[Link]("hello, world!").[Link]() # "Hello, world!" suits * 2 # "clubsclubs" "DiamondsDiamonds" "heartshearts" "SpadesSpades"

You might also like