How to convert a single column Pandas DataFrame into Series

后端 未结 3 1519
-上瘾入骨i
-上瘾入骨i 2020-12-19 07:21

I have the following data frame:

import pandas as pd
d = {\'gene\' : [\'foo\',\'bar\'],\'score\' : [4., 3.,]}
df = pd.DataFrame(d)
df.set_index(\'gene\',inpl         


        
相关标签:
3条回答
  • 2020-12-19 07:54

    Try swapping the indices in the brackets:

    df.iloc[:,0]
    

    This should work.

    0 讨论(0)
  • 2020-12-19 08:00

    Swapping the indices would solve the problem easily:

    In [64]: type(df.iloc[0:,])
    Out[64]: pandas.core.frame.DataFrame
    
    In [65]: df.iloc[[:,0] // Swaped the indices
    Out[65]:
            score
    gene
    foo       4
    bar       3
    
    0 讨论(0)
  • 2020-12-19 08:05
    s = df.squeeze()
    >>> s
    gene
    foo    4
    bar    3
    Name: score, dtype: float64
    

    To get it back to a dataframe:

    >>> s.to_frame()
          score
    gene       
    foo       4
    bar       3
    
    0 讨论(0)
提交回复
热议问题