2

I have a simple double bracket numpy array

import numpy as np import pandas as pd ar = np.array([[1,2,3,4]]) 

I am trying to convert it into a pandas series, but because of double brackets I am getting the below error.

pd.Series(ar) 

....

Exception: Data must be 1-dimensional 

how to achieve this in python

3 Answers 3

4

Using np.squeeze:

ar = np.array([[1,2,3,4]]) s = pd.Series(np.squeeze(ar)) s 

Output:

0 1 1 2 2 3 3 4 dtype: int64 
Sign up to request clarification or add additional context in comments.

Comments

4

The easiest way to do it:

pd.Series(ar[0]) 

Output:

0 1 1 2 2 3 3 4 dtype: int64 

1 Comment

Its pure array method, Thanks
2

Use numpy.ravel or numpy.flatten:

s = pd.Series(ar.ravel()) #alternetive #s = pd.Series(ar.flatten()) print (s) 0 1 1 2 2 3 3 4 dtype: int32 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.