Convert pandas data frame to series

后端 未结 6 1464
忘了有多久
忘了有多久 2020-11-30 21:52

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

6条回答
  •  醉梦人生
    2020-11-30 22:02

    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(_)
    
    

提交回复
热议问题