Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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!

1 Answer

0 votes
by (41.4k points)
edited by

Select the row positionally using iloc that will give 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'>

If you want to learn Python proogramming language for Data Science then you can watch this complete video tutorial:

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...