Why do data scientists often reshape data before analyzing it?
Think about how changing the shape of data can help reveal patterns.
Reshaping data helps organize it so that analysis tools can work better and insights become clearer.
Given this sales data, what is the output after pivoting by 'Month' with 'Product' as columns and 'Sales' as values?
import pandas as pd
data = pd.DataFrame({
'Month': ['Jan', 'Jan', 'Feb', 'Feb'],
'Product': ['A', 'B', 'A', 'B'],
'Sales': [100, 150, 200, 250]
})
pivoted = data.pivot(index='Month', columns='Product', values='Sales')
print(pivoted)Pivot changes rows into columns based on unique values.
The pivot method rearranges data so each product becomes a column with sales values for each month as rows.
What error does this code produce when trying to reshape data using melt?
import pandas as pd
data = pd.DataFrame({
'ID': [1, 2],
'Math': [90, 80],
'Science': [85, 95]
})
melted = pd.melt(data, id_vars=['ID'], value_vars=['Math', 'History'])
print(melted)Check if all columns in value_vars exist in the DataFrame.
The column 'History' does not exist in the DataFrame, causing a KeyError.
Which option shows the correct heatmap code to visualize sales data after pivoting by 'Month' and 'Product'?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.DataFrame({
'Month': ['Jan', 'Jan', 'Feb', 'Feb'],
'Product': ['A', 'B', 'A', 'B'],
'Sales': [100, 150, 200, 250]
})
pivoted = data.pivot(index='Month', columns='Product', values='Sales')Heatmaps require a 2D matrix-like input.
The pivoted DataFrame is a matrix suitable for heatmap visualization showing sales per product per month.
You have a dataset with daily temperature readings for multiple cities in wide format (each city is a column). You want to analyze temperature trends over time for each city. Which reshaping method is best to prepare the data?
Long format is better for time series analysis with multiple groups.
Melt reshapes wide data into long format, making it easier to analyze trends per city over time.