I'm Just trying to play will pivot
to transform the data Frame in a way i desired which pivot does but here its not working.
Any expert insight please.
DataFrame:
>>> df1
id item value
0 2225 prize 1.5
1 2225 unit kg
2 2225 prize 2.4
3 8187 unit lt
4 1401 stock 10
5 1401 prize 4.3
While running pivot
i see the below error..
>>> df1.pivot('id', 'item')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/frame.py", line 4359, in pivot
return pivot(self, index=index, columns=columns, values=values)
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/reshape/reshape.py", line 381, in pivot
return indexed.unstack(columns)
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/frame.py", line 4546, in unstack
return unstack(self, level, fill_value)
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/reshape/reshape.py", line 469, in unstack
return _unstack_frame(obj, level, fill_value=fill_value)
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/reshape/reshape.py", line 488, in _unstack_frame
fill_value=fill_value)
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/reshape/reshape.py", line 116, in __init__
self._make_selectors()
File "/grid/common/pkgs/python/v3.6.1/lib/python3.6/site-packages/pandas/core/reshape/reshape.py", line 154, in _make_selectors
raise ValueError('Index contains duplicate entries, '
ValueError: Index contains duplicate entries, cannot reshape
Even pivot_table
producing errors..
>>> df1.pivot_table(columns='item', values='value')
I tried to github link reference but didn't get it.
Desired should be like:
Value
item prize stock unit
id
2225 1.5 10 2.4 lt