R S*_*yed 10 python numpy pandas
我是熊猫和numpy的新手.我正在运行一个简单的程序
labels = ['a','b','c','d','e']
s = Series(randn(5),index=labels)
print(s)
Run Code Online (Sandbox Code Playgroud)
得到以下错误
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
Run Code Online (Sandbox Code Playgroud)
知道可能是什么问题吗?我正在尝试使用eclipse,而不是使用ipython笔记本.
我怀疑你的进口有误。
如果您将其添加到您的代码中:
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
Run Code Online (Sandbox Code Playgroud)
它运行良好。
也就是说,正如 @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)
Run Code Online (Sandbox Code Playgroud)
看来你需要numpy.random.randrandomfloats或numpy.random.randintfor random 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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30453 次 |
| 最近记录: |