0
0
Data Analysis Pythondata~10 mins

Adding and removing columns in Data Analysis Python - Interactive Code 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 'C' with all values set to 10.

Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df['C'] = [1]
print(df)
Drag options to blanks, or click blank then click option'
A'10'
B[10, 10]
Crange(2)
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning a single integer without a list causes the column to fill with that integer for all rows.
Assigning a string '10' creates a column of strings, not integers.
2fill in blank
medium

Complete the code to remove the column 'B' from the DataFrame.

Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df = df.[1]('B', axis=1)
print(df)
Drag options to blanks, or click blank then click option'
Adelete
Bremove
Cdrop
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove or delete which are not pandas DataFrame methods.
Forgetting to set axis=1 to specify column removal.
3fill in blank
hard

Fix the error in the code to add a new column 'D' as the sum of columns 'A' and 'B'.

Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df['D'] = 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 subtraction or multiplication instead of addition.
Using division which would not sum the columns.
4fill in blank
hard

Fill both blanks to create a new column 'E' with values from column 'A' multiplied by 2, but only for rows where 'B' is greater than 3.

Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [3, 4, 5]})
df['E'] = df['A'] [1] 2
filtered = df[df['B'] [2] 3]
print(filtered)
Drag options to blanks, or click blank then click option'
A*
B>
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using less than operator instead of greater than for filtering.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as uppercase column names and values as the mean of each column, but only for columns where the mean is greater than 2.

Data Analysis Python
import pandas as pd

df = pd.DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6], 'c': [0, 1, 1]})
result = { [1]: df[[2]].mean() for [3] in df.columns if df[[3]].mean() > 2 }
print(result)
Drag options to blanks, or click blank then click option'
Acol.upper()
Bcol
Dcol.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase for keys instead of uppercase.
Using wrong variable names in the comprehension.