我只是想玩 will pivot
以我希望的方式转换数据帧,但这里不起作用。
请任何专家见解。
数据框:
>>> 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
在运行 pivot
时我看到以下错误..
>>> 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
甚至 pivot_table
也会产生错误..
>>> df1.pivot_table(columns='item', values='value')
我试过 github link reference但没有得到它。
想要的应该是这样的:
Value
item prize stock unit
id
2225 1.5 10 2.4 lt
最佳答案
您应该尝试以下操作:
pd.pivot_table(df1, values='value', index=['id'], columns=['item'], aggfunc=np.sum)
正如 jezrael 在下面提到的,aggfunc 可以表示数值,也可以表示字符串。
关于python-3.x - pandas pivot 转换 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57687688/