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
If you have a pd.Series
object x
with index named 'Gene', you can use reset_index
and supply the name
argument:
df = x.reset_index(name='count')
Here's a demo:
x = pd.Series([2, 7, 1], index=['Ezh2', 'Hmgb', 'Irf1'])
x.index.name = 'Gene'
df = x.reset_index(name='count')
print(df)
Gene count
0 Ezh2 2
1 Hmgb 7
2 Irf1 1
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()
Source: Stackoverflow.com