Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assigning column names to a pandas series

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 respectively I tried

x_df = pd.DataFrame(x,columns = ['Gene','count']) 

but it does not work.The final form I want is

Gene Count Ezh2   2 Hmgb   7 Irf1   1 

Can you suggest how to do this

like image 258
Ssank Avatar asked Feb 13 '15 15:02

Ssank


People also ask

Do series have column names Pandas?

Simply, a Pandas Series is like an excel column. DataFrame = A collection of series. Each series name will be the column name.

How do I change the column name in series?

You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.


2 Answers

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 
like image 60
EdChum Avatar answered Sep 20 '22 19:09

EdChum


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() 
like image 44
Sealander Avatar answered Sep 17 '22 19:09

Sealander