3. String clean up Flashcards

1
Q

applying string method on df

A

df[“Col”].str.lower()

df[“Name”].str.lower().str.title()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

filter something in string

A

df[“Col”].str.contains(“water”)
startswith
endswith

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

remove white space

A

strip(), l/r strip()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

applyin string method on index

A

df.index = df.index.str.strip().str.title()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find first name in a full name string

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

expanding a split

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

merging

A

df[“New Name”] = df[“First Name”] + “ “ + df[“Last Name”].str.strip()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly