assigning column names to a pandas series

前端 未结 3 989
走了就别回头了
走了就别回头了 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:46

    You can also use the .to_frame() method.

    If it is a Series, I assume 'Gene' is already the index, and will remain the index after converting it to a DataFrame. The name argument of .to_frame() will name the column.

    x = x.to_frame('count')
    

    If you want them both as columns, you can reset the index:

    x = x.to_frame('count').reset_index()
    

提交回复
热议问题