The shape of cafeteria sales in long format
Predict the output · Medium · §4.1 Mapping table columns to a chart
This code uses melt() to turn a wide-format DataFrame of daily menu sales at a student cafeteria into a long-format DataFrame. Predict the output without running the code.
Code
import pandas as pd
daily = pd.DataFrame({
"day": ["Mon", "Tue", "Wed"],
"bibimbap": [40, 52, 46],
"ramen": [30, 26, 34],
})
meals = daily.melt(id_vars="day", value_vars=["bibimbap", "ramen"],
var_name="menu", value_name="sold")
print(meals.shape)
print(list(meals.columns))
print(meals.loc[4, "day"], meals.loc[4, "menu"], int(meals.loc[4, "sold"]))Solve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.