简体   繁体   中英

How to rename columns in DataFrame with pandas in Python

I have five stock portfolios that I have imported from Yahoo! finance and need to create a DataFrame with the closing prices for 2016 of all of the stocks. However, I'm struggling to label the columns with the corresponding stock names.

import pandas.io.data as web
import pandas_datareader.data as web
import pandas as pd

from pandas import Series, DataFrame
import numpy as np
import datetime

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(2016, 12, 31)

NFLX = web.DataReader("NFLX", 'yahoo', start, end)
AAPL = web.DataReader("AAPL", 'yahoo', start, end)
GOOGL = web.DataReader("GOOGL", 'yahoo', start, end)
FB = web.DataReader("FB", 'yahoo', start, end)
TSLA = web.DataReader("TSLA", 'yahoo', start, end)

df_NFLX = pd.DataFrame(NFLX['Close'])
df_AAPL = pd.DataFrame(AAPL['Close'])
df_GOOGL = pd.DataFrame(GOOGL['Close'])
df_FB = pd.DataFrame(FB['Close'])
df_TSLA = pd.DataFrame(TSLA['Close'])
frames = [df_NFLX, df_AAPL, df_GOOGL, df_FB, df_TSLA]
result = pd.concat(frames, axis = 1)
result = result.rename(columns = {'Two':'N'})
result

My code produces this - and I want to title each column accordingly.

Out[15]: 
                 Close       Close       Close       Close       Close
Date                                                                  
2016-01-04  109.959999  105.349998  759.440002  102.220001  223.410004
2016-01-05  107.660004  102.709999  761.530029  102.730003  223.429993
2016-01-06  117.680000  100.699997  759.330017  102.970001  219.039993
2016-01-07  114.559998   96.449997  741.000000   97.919998  215.649994
2016-01-08  111.389999   96.959999  730.909973   97.330002  211.000000
2016-01-11  114.970001   98.529999  733.070007   97.510002  207.850006
2016-01-12  116.580002   99.959999  745.340027   99.370003  209.970001

A simple way to patch up the code you've written is to just assign a list of names to df.columns .

df.columns = ['NFLX', 'AAPL', 'GOOGL', 'FB', 'TSLA']

However, there are ways to make large chunks of your code more concise which also allow you to specify the stock names as column names cleanly. I would go back to the beginning and (after defining start and end ) start by creating a list of the stock tickers you want to fetch.

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime(2016, 12, 31)
tickers = ['NFLX', 'AAPL', 'GOOGL', 'FB', 'TSLA']

Then you can construct all the data frames in a loop of some kind. If you want only the Close column, you can extract that column immediately, and in fact you can make a dict out of all these columns and then construct a DataFrame directly from that dict .

result = DataFrame({t: web.DataReader(t, 'yahoo', start, end)['Close']
                    for t in tickers})

An alternative would be to put all the stock data in a Panel , which would be useful if you might want to work with other columns.

p = pd.Panel({t: web.DataReader(t, 'yahoo', start, end) for t in tickers})

Then you can extract the Close figures with

result = p[:,:,'Close']

You'll notice it has the proper column labels automatically.

To rename the columns in the constructed table, you can change this:

df_NFLX = pd.DataFrame(NFLX['Close'])

to this:

df_NFLX = pd.DataFrame(NFLX['Close']).rename(columns={'Close': 'NFLX'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM