close

[Solved] pandas Series getting ‘Data must be 1-dimensional’ error

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error pandas Series getting ‘Data must be 1-dimensional’ error in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How pandas Series getting ‘Data must be 1-dimensional’ error Error Occurs?

Today I get the following error pandas Series getting ‘Data must be 1-dimensional’ error in python.

How To Solve pandas Series getting ‘Data must be 1-dimensional’ error Error ?

  1. How To Solve pandas Series getting 'Data must be 1-dimensional' error Error ?

    To Solve pandas Series getting 'Data must be 1-dimensional' error Error It seems you need numpy.random.rand for random floats or numpy.random.randint for random integers:

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

    To Solve pandas Series getting 'Data must be 1-dimensional' error Error It seems you need numpy.random.rand for random floats or numpy.random.randint for random integers:

Solution 1

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.

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)

Solution 2

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

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read