0
0
Pandasdata~10 mins

str.lower() and str.upper() in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert all names in the DataFrame to lowercase.

Pandas
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'CHARLIE']}
df = pd.DataFrame(data)
df['Name_lower'] = df['Name'].str.[1]()
print(df)
Drag options to blanks, or click blank then click option'
Acapitalize
Btitle
Clower
Dswapcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using str.upper() instead of str.lower()
Using str.capitalize() which only changes the first letter
2fill in blank
medium

Complete the code to convert all city names in the DataFrame to uppercase.

Pandas
import pandas as pd

data = {'City': ['Paris', 'london', 'New York']}
df = pd.DataFrame(data)
df['City_upper'] = df['City'].str.[1]()
print(df)
Drag options to blanks, or click blank then click option'
Aupper
Bcapitalize
Ctitle
Dswapcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using str.lower() instead of str.upper()
Using str.title() which capitalizes first letters of words
3fill in blank
hard

Fix the error in the code to convert the 'Fruit' column to lowercase.

Pandas
import pandas as pd

data = {'Fruit': ['Apple', 'BANANA', 'Cherry']}
df = pd.DataFrame(data)
df['Fruit_lower'] = df['Fruit'].str.[1]()
print(df)
Drag options to blanks, or click blank then click option'
Alowercase
Blower
Cto_lower
Dlower_case
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like lower_case or to_lower
Trying to use lowercase instead of lower
4fill in blank
hard

Fill both blanks to create a dictionary with fruit names in uppercase as keys and their lengths as values, only for fruits longer than 5 letters.

Pandas
fruits = ['apple', 'banana', 'cherry', 'date']
lengths = {fruit.[1](): len(fruit) for fruit in fruits if len(fruit) [2] 5}
print(lengths)
Drag options to blanks, or click blank then click option'
Aupper
B>
C<
Dlower
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() instead of upper() for keys
Using < instead of > in the condition
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase fruit names as keys, their lowercase versions as values, only for fruits with names longer than 4 letters.

Pandas
fruits = ['Apple', 'Banana', 'Fig', 'Grape']
result = {fruit.[1](): fruit.[2]() for fruit in fruits if len(fruit) [3] 4}
print(result)
Drag options to blanks, or click blank then click option'
Aupper
Blower
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up upper() and lower() for keys and values
Using < instead of > in the condition