0
0
Pandasdata~10 mins

Why reshaping data matters in Pandas - Test Your Understanding

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

Complete the code to reshape the DataFrame from wide to long format using pandas.

Pandas
long_df = df.melt(id_vars=[1])
Drag options to blanks, or click blank then click option'
A'variable'
B'id_vars'
C'value'
D['Name', 'Date']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single string instead of a list for id_vars
Confusing id_vars with value_vars
Passing column names that should be reshaped instead of kept fixed
2fill in blank
medium

Complete the code to pivot the DataFrame to wide format using pandas.

Pandas
wide_df = df.pivot(index=[1], columns='Month', values='Sales')
Drag options to blanks, or click blank then click option'
A'Month'
B'Store'
C'Sales'
D'Date'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the column for columns or values as the index
Confusing index and columns parameters
Passing a list instead of a single column name for index
3fill in blank
hard

Fix the error in the code to correctly stack the DataFrame columns into a Series.

Pandas
stacked = df.[1]()
Drag options to blanks, or click blank then click option'
Astack
Bmelt
Cunstack
Dpivot
Attempts:
3 left
💡 Hint
Common Mistakes
Using unstack instead of stack
Using melt which reshapes differently
Using pivot which requires different parameters
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
{word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length
Using the wrong condition syntax
Confusing the order of key and value in the comprehension
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts if count is greater than 0.

Pandas
result = [1]: [2] for [3] in data.items() if [2] > 0}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck, v
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name in the loop
Not converting the key to uppercase
Using the value as the loop variable