Pandas Get 1-D Data Series

I am new to pandas and numpy. I am running a simple program

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

get 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 could be the problem? I am trying to use eclipse without using ipython notebook.

+5
source share
2 answers

I suspect you have the wrong import.

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 is working fine.

However, as pointed out by @jezrael, it is better to practice importing modules rather than polluting the namespace.

Instead, your code should look like this.

decision

 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) 
+2
source

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 
+2
source

Source: https://habr.com/ru/post/1266273/


All Articles