Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 801
Ivan Avatar asked Jul 09 '17 21:07

Ivan


People also ask

How do you name a series in pandas?

rename() function is used to alter Series index labels or name for the given Series object. inplace : Whether to return a new Series. If True then value of copy is ignored. level : In case of a MultiIndex, only rename labels in the specified level.

How do you name columns in pandas series?

Method 1: Using rename() function One way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed.

Can you assign a name to the index of pandas series?

Pandas Series: rename() functionThe rename() function is used to alter Series index labels or name.


2 Answers

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
like image 118
Allen Avatar answered Oct 19 '22 18:10

Allen


This will do the job:

adjClose = symbolData.ix[:,10].rename("AlgoClose")
adjClose =pd.DataFrame(adjClose)
like image 26
Miriam Farber Avatar answered Oct 19 '22 16:10

Miriam Farber