Fix Python – Convert pandas data frame to series

Question

Asked By – user1357015

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) but it complains ValueError: cannot copy sequence with size 23 to array axis with dimension 1. It’s not smart enough to realize it’s still a “vector” in math terms.

Thanks!

Now we will see solution for issue: Convert pandas data frame to series


Answer

If you have a one column dataframe df, you can convert it to a series:

df.iloc[:,0]  # pandas Series

Since you have a one row dataframe df, you can transpose it so you’re in the previous case:

df.T.iloc[:,0]

This question is answered By – nicolass

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0