Ivan
Ivan

Reputation: 7746

How to name a Pandas Series

I have data as follows in a DataFrame symbolData:

              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 series with the Adj Close column, I would like to give it a name:

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

Upvotes: 15

Views: 26929

Answers (4)

Madhu Babu Adiki
Madhu Babu Adiki

Reputation: 51

You can also use the to_frame method with the name parameter:

symbolData.ix[:,10].to_frame(name='AlgoClose')

Upvotes: 1

Allen Qin
Allen Qin

Reputation: 19947

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.

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

Upvotes: 4

Max Power
Max Power

Reputation: 8954

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

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 DataFrame like in Miriam's answer.

Upvotes: 6

Miriam Farber
Miriam Farber

Reputation: 19634

This will do the job:

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

Upvotes: 14

Related Questions