Working With Text Data in Python
Working With Text Data in Python
# x
# Count the number of matches with .str.count()
# 0 0.123
suits.str.count("[ae]") # 0 1 2 2
# 1 4.567
Learn Python online at www.DataCamp.com # 2 8.901 # Locate the position of substrings with str.find()
suits.str.find("e") # -1 -1 1 4
df.style.format(precision = 1)
2 8.9
Throughout this cheat sheet, we’ll be using two pandas series named suits and # Extract capture groups with .str.extractall()
rock_paper_scissors. suits.str.extractall("([ae])(.)")
# 0 1
import pandas as pd
Splitting strings
# match
> # 1 0
# 2 0
a m
e a
suits.str.split(pat="")
suits.str.len() # Returns 5 8 6 6
suits.str.split(pat = "a")
# ["clubs"]
# ["Di", "monds"]
# ["Sp", "des"]
# 1 Di monds
# 2 he rts
# 3 Sp des
www.DataCamp.com
# Convert to uppercase with .str.upper()
# Combine two strings with +