Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas Series getting 'Data must be 1-dimensional' error

I'm new to pandas & numpy. I'm running a simple program

labels = ['a','b','c','d','e'] 
s = Series(randn(5),index=labels)
print(s)

getting the following error

    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

Any idea what can be the issue? I'm trying this using eclipse, not using ipython notebook.

like image 448
R Syed Avatar asked Apr 03 '17 19:04

R Syed


People also ask

Is a pandas series one-dimensional?

Technically, Pandas Series is a one-dimensional labeled array capable of holding any data type. So, in terms of Pandas DataStructure, A Series represents a single column in memory, which is either independent or belongs to a Pandas DataFrame.

Is pandas Series A two dimensional data structure?

Series. Pandas is a one-dimensional labeled array and capable of holding data of any type (integer, string, float, python objects, etc.)

How many dimensional is Panda series?

A Pandas Series is like a column in a table. It is a one-dimensional array holding data of any type.

Is series a one-dimensional array in Python?

Series is a one-dimensional array like structure with homogeneous data.


2 Answers

I suspect you have your imports wrong.

If you add this to your code:

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

It runs fine.

That said, and as pointed out by @jezrael, it's better practice to import the modules rather than pollute the namespace.

Your code should look like this instead.

solution

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)
like image 142
piRSquared Avatar answered Sep 18 '22 19:09

piRSquared


It seems you need numpy.random.rand for random floats or numpy.random.randint for 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

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
like image 44
jezrael Avatar answered Sep 20 '22 19:09

jezrael