【发布时间】:2020-04-19 07:09:18
【问题描述】:
这似乎是非常基础的知识,但尽管有一些数据处理的理论背景(通过其他软件),但我还是被卡住了。值得一提的是,我是 python 和 pandas 库的新手。
所以。我有一个数据框:
我的任务是将“系列名称”列的值作为单独的列(从长转换为宽)。我花了很长时间尝试不同的方法,但只得到错误。
例如:
mydata = mydata.pivot(index=['Country', 'Year'], columns='Series Name', values='Value')
我得到了一个错误:
...很多文字... ValueError: 传递值的长度是 2487175,索引意味着 2
有人可以指导我完成这个过程吗?谢谢。
这是为了代码 'mydata = mydata.pivot(index=['Country', 'Year'], columns='系列名称', values='Value')' 错误信息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-8169d6d374c7> in <module>
----> 1 mydata = mydata.pivot(index=['Country', 'Year'], columns='Series Name', values='Value')
~/anaconda3_501/lib/python3.6/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_501/lib/python3.6/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_501/lib/python3.6/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 2487175, index implies 2
【问题讨论】: