I am trying to pivot a dataframe in my code and got a value error "ValueError: Length of passed values is 153900, index implies 2". in attempt to solve the problem I copied the pandas example at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html and it gives the same error
df = pd.DataFrame({
"lev1": [1, 1, 1, 2, 2, 2],
"lev2": [1, 1, 2, 1, 1, 2],
"lev3": [1, 2, 1, 2, 1, 2],
"lev4": [1, 2, 3, 4, 5, 6],
"values": [0, 1, 2, 3, 4, 5]})
df.pivot(index=["lev1", "lev2"], columns=["lev3"],values="values")
which gives:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-32-ecb7df0ae99b> in <module>
----> 1 df.pivot(index=["lev1", "lev2"], columns=["lev3"],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, index, columns, 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, index, 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
Could it be a python version issue? I am running python 3.7.1 in JupyterLab in an Anaconda setup.