Ich bin neu bei Pandas & Numpy. Ich führe ein einfaches Programm aus
labels = ['a','b','c','d','e']
s = Series(randn(5),index=labels)
print(s)
der folgende Fehler wird angezeigt
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
Irgendeine Idee, was kann das Problem sein? Ich versuche dies mit Eclipse, nicht mit einem ipython-Notebook.
Ich vermute, Sie haben falsche Importe
Wenn Sie dies Ihrem Code hinzufügen
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
Es läuft gut.
Das heißt, und wie @jezrael hervorgehoben hat, ist es besser, die Module zu importieren, anstatt den Namespace zu verschmutzen.
Ihr Code sollte stattdessen so aussehen.
Lösung
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)
Es scheint, dass Sie numpy.random.Rand
für zufällige floats
oder numpy.random.randint
für zufällige integers
brauchen:
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