Complete the code to reshape the data using pandas melt function.
import pandas as pd data = pd.DataFrame({ 'Name': ['Alice', 'Bob'], 'Math': [90, 80], 'Science': [85, 88] }) reshaped = pd.melt(data, id_vars=['Name'], value_vars=[1]) print(reshaped)
The value_vars parameter specifies which columns to reshape into rows. Here, we want to reshape the 'Math' and 'Science' columns.
Complete the code to pivot the reshaped data back to wide format.
reshaped.pivot(index='Name', columns=[1], values='value')
The pivot function uses the 'variable' column created by melt as the new columns.
Fix the error in the code to correctly filter rows where score is greater than 85.
filtered = reshaped[reshaped['value'] [1] 85] print(filtered)
To filter rows where the score is greater than 85, use the '>' operator.
Fill both blanks to create a dictionary comprehension that maps each word to its length if length is greater than 3.
words = ['data', 'is', 'fun', 'science'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3.
Fill all three blanks to create a dictionary from data items where the value is greater than 50, with keys uppercased.
data = {'a': 40, 'b': 60, 'c': 70}
result = [1]: [2] for k, v in data.items() if v [3] 50}}The dictionary comprehension creates keys as uppercase versions of original keys using k.upper(), values as v, and filters items where v > 50.