String Operations Flashcards
Filter on a single string value
strings
2 alternatives
df_filtered = data[data.col1 == "A"] df_filtered = data.query("col1 == 'A'")
Filter on multiple string values
strings
3 alternatives
df_filtered = data[(data.col1 == "A") | (data.col1 == "B")] df_filtered = data[data.col1.isin(["A", "B"])] df_filtered = data.query("col1 == 'A' | col1 == 'B'")
Filter on the length of a string
strings
df_filtered = data[data.col1.str.len() > 4]
Filter on a substring
string
3 alternatives
df_filtered = data[data.col1.str.startswith("Jo")] df_filtered = data[data.col1.str.endswith("n")] df_filtered = data[data.col1.str.contains("ak")]
Make the string filtering case-insensitive
strings
df_filtered = data[data.col1.str.contains("ak", case = False)]
Make the string filtering ignore missing values
strings
df_filtered = data[data.col1.str.startswith("Jo", na = False)]
Filter on the type of character in the string
strings
df_filtered = data[data.col1.str.isalnum()]
Other keywords:
- all characters are upper-case : isupper()
- all characters are lower-case : islower()
- all characters are alphabetic : isalpha()
- all characters are numeric : isnumeric()
- all characters are digits : isdigit()
- all characters are decimal : isdecimal()
- all characters are whitespace : isspace()
- all characters are titlecase : istitle()
- all characters are alphanumeric : isalnum()
Change case of a string
myname = "Michael Lemay" str.upper(myname) str.title(myname) str.lower(myname)
String formatting
Traditional formatting
name = “John”
age = 30
message = “My name is {} and I’m {} years old.”.format(name, age)
message = f”My name is {name} and I’m {age} years old.”python
Split string
text = “This is a sample text.”
words = text.split()