[英]ValueError when creating a Series
When I run:当我运行时:
ser = pd.Series ( data =[ 100, 'Ninguno', 300, 'Texto', 5.3], index =[ 'pablo', 'juan', 'pedro', 'enrique'])
ser
the result gives me error:结果给了我错误:
ValueError: Length of passed values is 5, index implies 4.
ValueError:传递值的长度为 5,索引意味着 4。
A pandas Series is just a list or array that has an index. pandas 系列只是一个具有索引的列表或数组。 You are passing in the index and the data in your call above, but your index is not the same length as the data, so that is the problem.
您在上面的调用中传递了索引和数据,但是您的索引与数据的长度不同,所以这就是问题所在。 Below, I have added another element to the index to show a successful creation.
下面,我在索引中添加了另一个元素以显示成功创建。
From the look of your data, it isn't clear that you want to make a Series?从你的数据来看,你想制作一个系列还不清楚吗?
In [2]: data = [100, 'Ninguno', 300, 'Texto', 5.3]
In [3]: index = [ 'pablo', 'juan', 'pedro', 'enrique', 'other']
In [4]: ser = pd.Series(data=data, index=index)
In [5]: ser
Out[5]:
pablo 100
juan Ninguno
pedro 300
enrique Texto
other 5.3
dtype: object
In [6]: ser['pedro']
Out[6]: 300
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.