[英]pandas Series getting 'Data must be 1-dimensional' error
我是熊貓和numpy的新手。 我正在運行一個簡單的程序
labels = ['a','b','c','d','e']
s = Series(randn(5),index=labels)
print(s)
得到以下錯誤
s = Series(randn(5),index=labels) File "C:\Python27\lib\site-packages\pandas\core\series.py", line 243, in
__init__
raise_cast_failure=True) File "C:\Python27\lib\site-packages\pandas\core\series.py", line 2950, in
_sanitize_array
raise Exception('Data must be 1-dimensional') Exception: Data must be 1-dimensional
知道可能是什么問題嗎? 我正在嘗試使用eclipse,而不是使用ipython筆記本。
看來你需要numpy.random.rand
隨機floats
或numpy.random.randint
隨機integers
:
import pandas as pd
import numpy as np
np.random.seed(100)
labels = ['a','b','c','d','e']
s = pd.Series(np.random.randn(5),index=labels)
print(s)
a -1.749765
b 0.342680
c 1.153036
d -0.252436
e 0.981321
dtype: float64
np.random.seed(100)
labels = ['a','b','c','d','e']
s = pd.Series(np.random.randint(10, size=5),index=labels)
print(s)
a 8
b 8
c 3
d 7
e 7
dtype: int32
我懷疑你的進口錯了
如果將其添加到代碼中
from pandas import Series
from numpy.random import randn
labels = ['a','b','c','d','e']
s = Series(randn(5),index=labels)
print(s)
a 0.895322
b 0.949709
c -0.502680
d -0.511937
e -1.550810
dtype: float64
它運行正常。
也就是說,正如@jezrael所指出的,導入模塊而不是污染命名空間更好。
你的代碼應該是這樣的。
解
import pandas as pd
import numpy as np
labels = ['a','b','c','d','e']
s = pd.Series(np.random.randn(5),index=labels)
print(s)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.