Given the DataFrame df below, what will be the shape of the DataFrame after applying pivot?
import pandas as pd df = pd.DataFrame({ 'Date': ['2024-01-01', '2024-01-01', '2024-01-02', '2024-01-02'], 'City': ['NY', 'LA', 'NY', 'LA'], 'Temperature': [30, 60, 28, 65] }) pivoted = df.pivot(index='Date', columns='City', values='Temperature') print(pivoted.shape)
Think about how many unique dates and cities there are.
The pivot creates a table with unique dates as rows and unique cities as columns. There are 2 unique dates and 2 unique cities, so the shape is (2, 2).
What is the resulting DataFrame after melting the following data?
import pandas as pd df = pd.DataFrame({ 'ID': [1, 2], 'Math': [90, 80], 'Science': [85, 95] }) melted = pd.melt(df, id_vars=['ID'], var_name='Subject', value_name='Score') print(melted)
Melt turns columns into rows keeping the id_vars fixed.
The melt function converts columns 'Math' and 'Science' into rows under 'Subject' and their values under 'Score', repeating the 'ID' for each.
You have sales data for two products across three months in wide format. Which plot best shows the monthly sales comparison after reshaping the data to long format?
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'Month': ['Jan', 'Feb', 'Mar'], 'Product_A': [100, 120, 130], 'Product_B': [90, 110, 115] }) long_df = pd.melt(df, id_vars=['Month'], var_name='Product', value_name='Sales') plt.figure(figsize=(6,4)) for product in long_df['Product'].unique(): subset = long_df[long_df['Product'] == product] plt.plot(subset['Month'], subset['Sales'], marker='o', label=product) plt.legend() plt.title('Monthly Sales Comparison') plt.xlabel('Month') plt.ylabel('Sales') plt.tight_layout() plt.show()
Think about how to compare trends over time for multiple products.
The line plot clearly shows sales trends over months for each product after reshaping data to long format.
What error will this code raise?
import pandas as pd df = pd.DataFrame({ 'Date': ['2024-01-01', '2024-01-02'], 'City': ['NY', 'LA'], 'Temperature': [30, 60] }) pivoted = df.pivot(index='Date', columns='Temperature', values='City') print(pivoted)
Check if the index and columns have unique values.
Since 'Date' and 'Temperature' columns have unique values, pivot works without error and prints the reshaped DataFrame.
You have a DataFrame with daily sales data for multiple stores in wide format, where each store is a column. You want to prepare the data for time series analysis that requires a single sales column and a store identifier column. Which reshaping method should you use?
Think about turning columns into rows while keeping identifiers.
melt is used to convert wide data into long format by turning columns into rows, which is needed for time series analysis with a single sales column and store identifier.