cereal_long| name | mfr | nutrition | value | |
|---|---|---|---|---|
| 0 | Special K | K | calories | 110 |
| 1 | Special K | K | protein | 6 |
| 2 | Apple Jacks | K | calories | 110 |
| ... | ... | ... | ... | ... |
| 7 | Cheerios | G | protein | 6 |
| 8 | Wheaties | G | calories | 100 |
| 9 | Wheaties | G | protein | 3 |
10 rows × 4 columns
| name | mfr | nutrition | value | |
|---|---|---|---|---|
| 0 | Special K | K | calories | 110 |
| 1 | Special K | K | protein | 6 |
| 2 | Apple Jacks | K | calories | 110 |
| ... | ... | ... | ... | ... |
| 7 | Cheerios | G | protein | 6 |
| 8 | Wheaties | G | calories | 100 |
| 9 | Wheaties | G | protein | 3 |
10 rows × 4 columns
cereal_wider = cereal_long.pivot_table(index=['name','mfr'], columns='nutrition', values='value')
cereal_wider| nutrition | calories | protein | |
|---|---|---|---|
| name | mfr | ||
| Apple Jacks | K | 110.0 | 2.0 |
| Cheerios | G | 110.0 | 6.0 |
| Raisin Bran | K | 120.0 | 3.0 |
| Special K | K | 110.0 | 6.0 |
| Wheaties | G | 100.0 | 3.0 |
| name | mfr | nutrition | value | |
|---|---|---|---|---|
| 0 | Special K | K | calories | 100 |
| 1 | Special K | K | calories | 130 |
| 2 | Special K | K | protein | 6 |
| 3 | Apple Jacks | K | calories | 110 |
| 4 | Apple Jacks | K | protein | 2 |
ValueError: Index contains duplicate entries, cannot reshape
Detailed traceback:
File "<string>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 5923, in pivot
return pivot(self, index=index, columns=columns, values=values)
File "/usr/local/lib/python3.7/site-packages/pandas/core/reshape/pivot.py", line 450, in pivot
return indexed.unstack(columns)
File "/usr/local/lib/python3.7/site-packages/pandas/core/series.py", line 3550, in unstack
return unstack(self, level, fill_value)
File "/usr/local/lib/python3.7/site-packages/pandas/core/reshape/reshape.py", line 179, in _make_selectors
raise ValueError("Index contains duplicate entries, cannot reshape")
Attribution: Nikolay Grozev, Reshaping in Pandas - Pivot, Pivot-Table, Stack, and Unstack explained with Pictures
| nutrition | calories | protein | |
|---|---|---|---|
| name | mfr | ||
| Apple Jacks | K | 110.0 | 2.0 |
| Special K | K | 115.0 | 6.0 |
0 True
1 True
2 False
3 False
4 False
dtype: bool
0 True
1 True
2 False
3 False
4 False
dtype: bool
| name | mfr | nutrition | value | |
|---|---|---|---|---|
| 0 | Special K | K | calories | 100 |
| 1 | Special K | K | calories | 130 |
| 2 | Special K | K | protein | 6 |
| 3 | Apple Jacks | K | calories | 110 |
| 4 | Apple Jacks | K | protein | 2 |