如何命名 Pandas 系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45001434/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:57:35  来源:igfitidea点击:

How to name a Pandas Series

pythonpandasdataframe

提问by Ivan

I have data as follows in a DataFramesymbolData:

我在 a 中有如下数据DataFramesymbolData

              Open      High       Low   Close      Volume  Ex-Dividend  Split Ratio   Adj. Open   Adj. High    Adj. Low  Adj. Close  Adj. Volume
Date
1980-12-12   28.75   28.8700   28.7500   28.75   2093900.0          0.0          1.0    0.424421    0.426193    0.424421    0.424421  117258400.0

When I get just the serieswith the Adj Closecolumn, I would like to give it a name:

当我得到series这个Adj Close列时,我想给它一个名字:

adjClose = symbolData.ix[:,10]

Right now, the above looks like

现在,上面看起来像

Date
1980-12-12      0.424421

I would like it to look like

我希望它看起来像

                AlgoClose
Date                   
1980-12-12      0.424421

回答by Miriam Farber

This will do the job:

这将完成这项工作:

adjClose = symbolData.ix[:,10].rename("AlgoClose")
adjClose =pd.DataFrame(adjClose)

回答by Max Power

after you define your series with ix, you can set its name with:

使用 定义系列后ix,您可以使用以下命令设置其名称:

adjClose.name = 'adjClose'

or, you could keep the original column-name when you define the series, like this:

或者,您可以在定义系列时保留原始列名,如下所示:

adjClose = symbolData['Adj. Close']

this 'named series' won't display quite how you asked for though, it will display like:

这个“命名系列”不会完全按照你的要求显示,它会显示如下:

Date
1980-12-12      0.424421
Name: adjclose, dtype: float64

if it's more important to display as you want, rather than to keep it a series, than convert it to a one-column DataFramelike in Miriam's answer.

如果根据需要显示更重要,而不是将其保留为系列,那么将其转换为DataFrame像 Miriam 的答案中的一列。

回答by Allen

You can rename the Series and then use .to_frame to convert it to a dataframe. Also, it's better to use iloc instead of ix as it's going to be deprecated in the future.

您可以重命名系列,然后使用 .to_frame 将其转换为数据帧。此外,最好使用 iloc 而不是 ix,因为它将来会被弃用。

df.iloc[:,10].rename('AlgoClose').to_frame()
Out[20]: 
            AlgoClose
Date                 
1980-12-12   0.424421

回答by Madhu Babu Adiki

You can also use the to_frame(name)function with the nameparameter: symbolData.ix[:,10].to_frame(name='AlgoClose')

您还可以使用to_frame(name)带有name参数的函数: symbolData.ix[:,10].to_frame(name='AlgoClose')