0
0
Pandasdata~10 mins

Creating new columns in Pandas - Interactive Practice

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

Complete the code to add a new column 'total' as the sum of columns 'a' and 'b'.

Pandas
import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df['total'] = df['a'] [1] df['b']
print(df)
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes subtraction.
Using '*' or '/' will multiply or divide instead of adding.
2fill in blank
medium

Complete the code to create a new column 'is_high' that is True if 'score' is greater than 50.

Pandas
import pandas as pd
df = pd.DataFrame({'score': [45, 60, 30]})
df['is_high'] = df['score'] [1] 50
print(df)
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will check for less than, which is incorrect here.
Using '==' checks for equality, not greater than.
3fill in blank
hard

Fix the error in the code to create a new column 'double' that doubles the values in 'num'.

Pandas
import pandas as pd
df = pd.DataFrame({'num': [1, 2, 3]})
df['double'] = df['num'] [1] 2
print(df)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds 2 instead of doubling.
Using '-' or '/' will not double the values.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Pandas
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2] > 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Checking the word instead of its length in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their scores only if score is above 50.

Pandas
data = {'apple': 40, 'banana': 60, 'cherry': 70}
result = { [1]: [2] for [3], score in data.items() if score > 50 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Bscore
Cword
Dfruit
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in the loop.
Not converting words to uppercase for keys.