Menu
Coddy logo textTech

Modify Strings

Lesson 12 of 19 in Coddy's Pandas Analytics course.

Strings are a bit more tricky to work with because you can't just add 5 or compare it to a number. For that, Pandas has a special tool .str that allows us to use many methods that Python strings support such as: .upper(), .lower(), .split() .len(), etc.

To convert a string column to uppercase letters:

df["existing_column"] = df["existing_column"].str.upper()

To chain multiple methods, we need to use multiple .str because each method returns a series object:

.str.upper().str.split().str.lower()...

To create a first_name and a last_name column from a full_name:

df["first_name"] = df["full_name"].str.split(" ").str[0]
df["last_name"] = df["full_name"].str.split(" ").str[1] 
challenge icon

Challenge

Easy

The CSV file stats.csv contains information about stats.

Here is the first 5 lines of the file:

ID,COUNTRY,COLOR,SKILL,SKILL_POINTS,UTILIZATION,IS_VALID,CATEGORY
1,France,Signal violet,marksmanship,14,0.1924,1,SHOW
2,Solomon Islands,Pearl violet,crocheting,4,0.6108,1,TREE
3,Germany,Bottle green,calligraphy,12,0.88646,0,SHOW
4,Mauritania,Fawn brown,paper cutting,9,0.058,1,JAPE
  • Modify the COLOR column to hold only the last word of the COLOR column.
  • Add a column named COUNTRY_LENGTH that holds the number of characters in the COUNTRY  column.
  • Filter all rows where the SKILL column contains only one word (for example, "wood chopper" has two words).

Store the result in the df variable.

Try it yourself

# pandas as pd is already imported
df = pd.read_csv("./stats.csv")
# Write your code below

All lessons in Pandas Analytics