assigning column names to a pandas series

前端 未结 3 987
走了就别回头了
走了就别回头了 2020-12-13 13:18

I have a pandas series

object x
Ezh2   2
Hmgb   7
Irf1   1

I want to save this as a dataframe with column names Gene and Count respectivel

3条回答
  •  情话喂你
    2020-12-13 13:59

    You can create a dict and pass this as the data param to the dataframe constructor:

    In [235]:
    
    df = pd.DataFrame({'Gene':s.index, 'count':s.values})
    df
    Out[235]:
       Gene  count
    0  Ezh2      2
    1  Hmgb      7
    2  Irf1      1
    

    Alternatively you can create a df from the series, you need to call reset_index as the index will be used and then rename the columns:

    In [237]:
    
    df = pd.DataFrame(s).reset_index()
    df.columns = ['Gene', 'count']
    df
    Out[237]:
       Gene  count
    0  Ezh2      2
    1  Hmgb      7
    2  Irf1      1
    

提交回复
热议问题