0
0
Pandasdata~10 mins

Wide to long format conversion 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 the DataFrame from wide to long format using pandas.

Pandas
long_df = pd.melt(df, id_vars=['id'], value_vars=[1])
Drag options to blanks, or click blank then click option'
A['id']
B['age']
C['score3']
D['score1', 'score2']
Attempts:
3 left
💡 Hint
Common Mistakes
Including 'id' in value_vars causes incorrect reshaping.
Not specifying value_vars leads to all columns being unpivoted.
2fill in blank
medium

Complete the code to rename the variable and value columns after melting.

Pandas
long_df = pd.melt(df, id_vars=['id'], value_vars=['score1', 'score2'], var_name=[1], value_name='score')
Drag options to blanks, or click blank then click option'
A'test'
B'subject'
C'score'
D'variable'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for var_name and value_name causes confusion.
Leaving var_name as default may be unclear in the output.
3fill in blank
hard

Fix the error in the code to correctly convert wide to long format with multiple id_vars.

Pandas
long_df = pd.melt(df, id_vars=[1], value_vars=['score1', 'score2'])
Drag options to blanks, or click blank then click option'
A['age']
B['id', 'age']
C['id']
D['score1', 'score2']
Attempts:
3 left
💡 Hint
Common Mistakes
Only including one id_var when multiple are needed.
Putting value columns in id_vars causes errors.
4fill in blank
hard

Fill both blanks to create a long DataFrame with custom variable and value column names.

Pandas
long_df = pd.melt(df, id_vars=['id'], value_vars=['score1', 'score2'], var_name=[1], value_name=[2])
Drag options to blanks, or click blank then click option'
A'test'
B'score'
C'value'
D'variable'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping var_name and value_name values.
Using default names that are unclear.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.

Pandas
lengths = { [1] : [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using length comparison incorrectly.