如何解决pandas.DataFrame.pivot 示例上的 ValueError
我试图在我的代码中旋转一个数据帧,但得到一个值错误“ValueError:传递的值的长度是 153900,索引意味着 2”。为了解决这个问题,我在 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html 复制了熊猫示例,但它给出了相同的错误
df = pd.DataFrame({
"lev1": [1,1,2,2],"lev2": [1,"lev3": [1,"lev4": [1,3,4,5,6],"values": [0,5]})
df.pivot(index=["lev1","lev2"],columns=["lev3"],values="values")
给出:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-ecb7df0ae99b> in <module>
----> 1 df.pivot(index=["lev1",values="values")
~\Anaconda3\lib\site-packages\pandas\core\frame.py in pivot(self,index,columns,values)
5192 """
5193 from pandas.core.reshape.reshape import pivot
-> 5194 return pivot(self,index=index,columns=columns,values=values)
5195
5196 _shared_docs['pivot_table'] = """
~\Anaconda3\lib\site-packages\pandas\core\reshape\reshape.py in pivot(self,values)
412 else:
413 indexed = self._constructor_sliced(self[values].values,--> 414 index=index)
415 return indexed.unstack(columns)
416
~\Anaconda3\lib\site-packages\pandas\core\series.py in __init__(self,data,dtype,name,copy,fastpath)
260 'Length of passed values is {val},'
261 'index implies {ind}'
--> 262 .format(val=len(data),ind=len(index)))
263 except TypeError:
264 pass
ValueError: Length of passed values is 6,index implies 2
会不会是python版本的问题?我在 Anaconda 设置中的 JupyterLab 中运行 python 3.7.1。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。