exam questions

Exam DP-100 All Questions

View all questions & answers for the DP-100 exam

Exam DP-100 topic 3 question 18 discussion

Actual exam question from Microsoft's DP-100
Question #: 18
Topic #: 3
[All DP-100 Questions]

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:

Show Suggested Answer Hide Answer
Suggested Answer:

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
meswapnilspal
Highly Voted 4 years, 5 months ago
if salesData contains data in pivoted form ,the syntax should be, newsalesData = pd.melt(salesData, id_vars = ...............
upvoted 58 times
...
FuzzyF
Highly Voted 4 years ago
Just tried out: pd.melt(salesData, id_vars=['shop'], value_vars=[2017, 2018], var_name='year') works as desired. [2017, 2018] for value_vars is correct. Side note: without 'var_name' parameter a default name is given to column 2.
upvoted 40 times
...
evangelist
Most Recent 5 months ago
salesData = pd.melt(salesData, id_vars='shop', value_vars=['2017', '2018'])
upvoted 1 times
...
kay1101
5 months, 2 weeks ago
I think this question is missing a step of: whateverdfname = pd.DataFrame(....) then we can use salesData =pd.melt(whateverdfname, ...,...) so the second and third answer are correct, first box not sure.
upvoted 1 times
...
Ahmed_Gehad
1 year, 3 months ago
The answer is wrong. It shall be pd.melt(salesData, id_vars=['shop'], value_vars=['2017', '2018'])
upvoted 2 times
...
PI_Team
1 year, 3 months ago
import pandas as pd salesData = pd.DataFrame({ 'shop': ['ShopX', 'ShopY', 'ShopZ'], '2017': [34, 65, 48], '2018': [25, 76, 55] }) meltedData = pd.melt(salesData, id_vars=['shop'], value_vars=['2017', '2018'], var_name='year', value_name='value')
upvoted 2 times
...
silva_831
2 years ago
Please do not mislead the people. The correct answer should be as below: 1. salesData 2. shop 3. [2017, 2018]
upvoted 14 times
...
MohammadKhubeb
2 years, 9 months ago
why not YEAR, because we are giving the values of col YEAR i.e., 2017,... ?
upvoted 1 times
...
trickerk
3 years, 3 months ago
Correct answer: salesData shop [2017, 2018]
upvoted 9 times
...
ljljljlj
3 years, 3 months ago
On exam 2021/7/10
upvoted 6 times
...
ali25
3 years, 7 months ago
import pandas as pd salesData = pd.DataFrame({'shop': {0: 'shopx', 1: 'shopy', 2: 'shopz'}, '2017': {0: '34', 1: '65', 2: '48'}, '2018': {0: '25', 1: '76', 2: '55'}}) salesData salesData = salesData.reset_index() salesData salesData = pd.melt(salesData, id_vars =['shop'], value_vars=['2017', '2018']) salesData
upvoted 8 times
...
[Removed]
3 years, 7 months ago
import pandas as pd df = pd.DataFrame({'shop': {0: 'shopx', 1: 'shopy', 2: 'shopz'}, '2017': {0: '34', 1: '65', 2: '48'}, '2018': {0: '25', 1: '76', 2: '55'}}) df pd.melt(df, id_vars =['shop'], value_vars =['2017', '2018']) /////////////////////////////////////////////////////// ////////////////////////////////////////////////////// shop variable value 0 shopx 2017 34 1 shopy 2017 65 2 shopz 2017 48 3 shopx 2018 25 4 shopy 2018 76 5 shopz 2018 55
upvoted 5 times
...
Raxy
3 years, 10 months ago
Is this a test question that will be included in real exam?
upvoted 3 times
...
alphmzla
3 years, 10 months ago
Just tried df = pd.melt(df, id_vars= ['Shop'], value_vars=['2017', '2018']), it returns desired outcome
upvoted 1 times
...
worker_3141592
4 years ago
import pandas as pd salesData = pd.DataFrame({'shop': {0: 'Shop X', 1: 'Shop Y', 2: 'Shop Z'}, '2017': {0: 34, 1: 65, 2: 48}, '2018': {0: 25, 1: 76, 2: 55}}) salesData = pd.melt(salesData,id_vars='shop',value_vars=['2017','2018']) print(salesData) ----------------------------------------------------------------------------------------------------------------------- shop variable value 0 Shop X 2017 34 1 Shop Y 2017 65 2 Shop Z 2017 48 3 Shop X 2018 25 4 Shop Y 2018 76 5 Shop Z 2018 55
upvoted 4 times
dsyouness
4 years ago
but if we use dataFrame instead of salesData : dataFrame = pd.DataFrame({'shop': {0: 'Shop X', 1: 'Shop Y', 2: 'Shop Z'}, '2017': {0: 34, 1: 65, 2: 48}, '2018': {0: 25, 1: 76, 2: 55}})
upvoted 1 times
...
...
SN22
4 years ago
salesData is correct
upvoted 2 times
...
ARC
4 years, 2 months ago
pd.melt(salesData, id_vars=['shop'], value_vars=['year'])
upvoted 2 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago