简体   繁体   English

如何在Python中使用熊猫重命名DataFrame中的列

[英]How to rename columns in DataFrame with pandas in Python

I have five stock portfolios that I have imported from Yahoo! 我从Yahoo!导入了五种股票投资组合! finance and need to create a DataFrame with the closing prices for 2016 of all of the stocks. 财务,并且需要创建一个DataFrame,其中包含所有股票2016年的收盘价。 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

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. 我将回到开始,并(在定义startend )开始,创建要获取的股票行情清单。

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 . 如果只需要Close列,则可以立即提取该列,实际上您可以从所有这些列中创建一个dict ,然后直接从该dict构造一个DataFrame

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. 一种替代方法是将所有库存数据放入Panel ,如果您想使用其他列,这将很有用。

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

Then you can extract the Close figures with 然后你就可以提取Close与数字

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'})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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