Python 将列名分配给熊猫系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28503445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assigning column names to a pandas series
提问by Ssank
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
我想将它保存为一个数据框,列名分别为 Gene 和 Count 我试过
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
你能建议如何做到这一点吗
采纳答案by EdChum
You can create a dict and pass this as the data param to the dataframe constructor:
您可以创建一个 dict 并将其作为数据参数传递给数据框构造函数:
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:
或者,您可以从系列创建 df,您需要调用,reset_index
因为将使用索引,然后重命名列:
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
回答by Sealander
You can also use the .to_frame()
method.
您也可以使用该.to_frame()
方法。
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.
如果它是一个系列,我假设 'Gene' 已经是索引,并且在将其转换为 DataFrame 后仍将是索引。的name
参数.to_frame()
将命名列。
x = x.to_frame('count')
If you want them both as columns, you can reset the index:
如果您希望它们都作为列,则可以重置索引:
x = x.to_frame('count').reset_index()
回答by jpp
If you have a pd.Series
object x
with index named 'Gene', you can use reset_index
and supply the name
argument:
如果您有一个索引名为“Gene”的pd.Series
对象x
,您可以使用reset_index
并提供name
参数:
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