user1357015 - 7 months ago 258

Python Question

I'm somewhat new to pandas. I have a pandas data frame that is 1 row by 23 columns.

I want to convert this into a series? I'm wondering what the most pythonic way to do this is?

I've tried

`pd.Series(myResults)`

`ValueError: cannot copy sequence with size 23 to array axis with dimension 1`

Thanks!

Answer

It's not smart enough to realize it's still a "vector" in math terms.

Say rather that it's smart enough to recognize a difference in dimensionality. :-)

I think the simplest thing you can do is select that row positionally using `iloc`

, which gives you a Series with the columns as the new index and the values as the values:

```
>>> df = pd.DataFrame([list(range(5))], columns=["a{}".format(i) for i in range(5)])
>>> df
a0 a1 a2 a3 a4
0 0 1 2 3 4
>>> df.iloc[0]
a0 0
a1 1
a2 2
a3 3
a4 4
Name: 0, dtype: int64
>>> type(_)
<class 'pandas.core.series.Series'>
```

Source (Stackoverflow)