3. String clean up Flashcards
applying string method on df
df[“Col”].str.lower()
df[“Name”].str.lower().str.title()
filter something in string
df[“Col”].str.contains(“water”)
startswith
endswith
remove white space
strip(), l/r strip()
applyin string method on index
df.index = df.index.str.strip().str.title()
find first name in a full name string
df[“Name”].str.split(“,”).str.get(0)
note this “get” is pandas specific
df[“Name”].str.split(“,”).str.get(1).str.strip().str.split().str.get(0)
expanding a split
df[“Name”].str.split(“,”, expand = True)
return new df
df[[“First Name”,”Last Name”]] = df[“Name”].str.split(“,”, expand = True)
df[[“First Name”,”Last Name”]] = df[“Name”].str.split(“,”, expand = True, n=1)
merging
df[“New Name”] = df[“First Name”] + “ “ + df[“Last Name”].str.strip()