Home » Microsoft » DP-100 v.2 » HOTSPOT
HOTSPOT
You have a Python data frame named salesData in the following format:
The data frame must be unpivoted to a long data format as follows:
You need to use the pandas.melt() function in Python to perform the transformation.
How should you complete the code segment? To answer, select the appropriate options in the answer area.
NOTE: Each correct selection is worth one point.
Hot Area:
Correct Answer:
Explanation/Reference:
Explanation:
Box 1: dataFrame Syntax: pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name=’value’, col_level=None)[source] Where frame is a DataFrame Box 2: shop Paramter id_vars id_vars : tuple, list, or ndarray, optional Column(s) to use as identifier variables.
Box 3: [‘2017′,’2018’] value_vars : tuple, list, or ndarray, optional Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.
Example:
df = pd.DataFrame({‘A’: {0: ‘a’, 1: ‘b’, 2: ‘c’},
… ‘B’: {0: 1, 1: 3, 2: 5},
… ‘C’: {0: 2, 1: 4, 2: 6}}) pd.melt(df, id_vars=[‘A’], value_vars=[‘B’, ‘C’]) A variable value
0 a B 1
1 b B 3
2 c B 5
3 a C 2
4 b C 4
5 c C 6 References:
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.melt.html