Computer scienceData scienceInstrumentsPandasData analysis with pandas

Reshaping and Pivot Tables

Melt wine... what?

Report a typo

Here we have the table with the number of wine bottles by countries and varieties:

df

Output:



+----+--------------------------+-------------+-------------+----------+----------+---------+---------+------+
|    | variety                  |   Argentina |   Australia |   Canada |   France |   Italy |   Spain |   US |
|----+--------------------------+-------------+-------------+----------+----------+---------+---------+------|
|  0 | Bordeaux-style Red Blend |          79 |          28 |       15 |      862 |       2 |       6 | 1235 |
|  1 | Cabernet Sauvignon       |         391 |         155 |        3 |       30 |      29 |      42 | 2463 |
|  2 | Chardonnay               |         204 |         206 |       14 |     1587 |     110 |      37 | 2778 |
|  3 | Merlot                   |          34 |           7 |        4 |       67 |      47 |      11 |  703 |
|  4 | Pinot Noir               |          68 |          72 |       14 |     1121 |       1 |      11 | 5449 |
|  5 | Red Blend                |         209 |          60 |        5 |      218 |    1605 |     683 | 1985 |
|  6 | Rosé                     |          30 |          18 |        2 |     1120 |       4 |     106 |  510 |
|  7 | Sauvignon Blanc          |          52 |          19 |        4 |      616 |      18 |      16 |  669 |
|  8 | Syrah                    |          46 |          15 |        9 |      110 |      49 |      27 | 1728 |
|  9 | White Blend              |          30 |          12 |        5 |      167 |     295 |     236 |  410 |
+----+--------------------------+-------------+-------------+----------+----------+---------+---------+------+

You have to reshape into a long format (3 columns) and save the result to df_long.

Tip: At the end you should get the dataset with columns containing varieties, countries and its values. Try melt() function.

Write a program in Python 3
import pandas as pd

wine_sample = {'variety': {0: 'Bordeaux-style Red Blend', 1: 'Cabernet Sauvignon', 2: 'Chardonnay', 3: 'Merlot',
4: 'Pinot Noir', 5: 'Red Blend', 6: 'Rosé', 7: 'Sauvignon Blanc', 8: 'Syrah', 9: 'White Blend'},
'Argentina': {0: 79, 1: 391, 2: 204, 3: 34, 4: 68, 5: 209, 6: 30, 7: 52, 8: 46, 9: 30},
'Australia': {0: 28, 1: 155, 2: 206, 3: 7, 4: 72, 5: 60, 6: 18, 7: 19, 8: 15, 9: 12},
'Canada': {0: 15, 1: 3, 2: 14, 3: 4, 4: 14, 5: 5, 6: 2, 7: 4, 8: 9, 9: 5},
'France': {0: 862, 1: 30, 2: 1587, 3: 67, 4: 1121, 5: 218, 6: 1120, 7: 616, 8: 110, 9: 167},
'Italy': {0: 2, 1: 29, 2: 110, 3: 47, 4: 1, 5: 1605, 6: 4, 7: 18, 8: 49, 9: 295},
'Spain': {0: 6, 1: 42, 2: 37, 3: 11, 4: 11, 5: 683, 6: 106, 7: 16, 8: 27, 9: 236},
'US': {0: 1235, 1: 2463, 2: 2778, 3: 703, 4: 5449, 5: 1985, 6: 510, 7: 669, 8: 1728, 9: 410}}

df = pd.DataFrame(wine_sample)

# your code here
df_long = ...
___

Create a free account to access the full topic