0
0
Data Analysis Pythondata~10 mins

One-hot encoding 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 import the pandas library.

Data Analysis Python
import [1] as pd
Drag options to blanks, or click blank then click option'
Anumpy
Bseaborn
Cmatplotlib
Dpandas
Attempts:
3 left
💡 Hint
Common Mistakes
Importing numpy instead of pandas
Forgetting to use 'as pd'
2fill in blank
medium

Complete the code to create a DataFrame from a dictionary.

Data Analysis Python
df = pd.DataFrame({'color': ['red', 'blue', 'green']})
print(df.[1]())
Drag options to blanks, or click blank then click option'
Ahead
Btail
Cinfo
Ddescribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using tail() to see the first rows
Using info() which shows summary info
3fill in blank
hard

Fix the error in the code to apply one-hot encoding using pandas.

Data Analysis Python
one_hot = pd.get_dummies(df['color'], [1]=True)
print(one_hot)
Drag options to blanks, or click blank then click option'
Aremove
Bdrop
Cdrop_first
Dexclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'drop' or 'remove'
Not using any parameter and causing redundant columns
4fill in blank
hard

Fill both blanks to create a one-hot encoded DataFrame and join it with the original.

Data Analysis Python
one_hot = pd.get_dummies(df['color'], [1]=False)
df_new = df.[2](one_hot, axis=1)
print(df_new)
Drag options to blanks, or click blank then click option'
Adrop_first
Bconcat
Cappend
Dmerge
Attempts:
3 left
💡 Hint
Common Mistakes
Using append which adds rows, not columns
Using merge which requires keys
5fill in blank
hard

Fill all three blanks to create a one-hot encoded DataFrame, drop the original column, and display the result.

Data Analysis Python
one_hot = pd.get_dummies(df['color'], [1]=True)
df = df.[2]('color', axis=[3])
df_final = pd.concat([df, one_hot], axis=1)
print(df_final)
Drag options to blanks, or click blank then click option'
Adrop_first
Bdrop
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 which drops rows instead of columns
Not dropping the original column before concatenation