26

I have a ndarray like this one:

In [75]:
z_r
Out[75]:
array([[ 0.00909254],
       [ 0.02390291],
       [ 0.02998752]])

In here, I want to ask how to convert those things to series, the desired output is like this:

0   0.00909254
1   0.02390291
2   0.02998752
0

4 Answers 4

32

You can use this one:

my_list = map(lambda x: x[0], z_r)
ser = pd.Series(my_list)
In [86]:
ser
Out[86]:
0      0.009093
1      0.023903
2      0.029988

Actually, your question is how to convert to series ^^

Sign up to request clarification or add additional context in comments.

1 Comment

@ markov zain: I'd rename this to 'Ndarray of Ndarrays to Series' because it describes your data more accurately. If it is just an ndarray of values, it does not work. Ex: array([ 0.00909254, 0.02390291, 0.02998752]), does not work with this code.
13

pd.Series(my_ndarray)

no need to convert it first to a list then to series.

1 Comment

that doesnt work as that data isn't 1-dimensional
5
z_r.tolist()

                 

4 Comments

just a quick note: this doesn't create a pandas series it converts the ndarray to a list: [[0.00909254], [0.02390291], [0.02998752]].
Just wrap it with pd.Series()
I found your method better and more readable. Is the accepted answer converting the array to 1d array and then convert it to series? Is map(lambda x: x[0], z_r) doing the same thing as tolist()? Thanks
@BowenLiu Try it.
0

Using list comprehension instead of map:

my_series = pd.Series([x[0] for x in z_r])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.