如何在 Python 中使用 Pandas 重命名 DataFrame 中的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41668130/
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
How to rename columns in DataFrame with pandas in Python
提问by 6114617
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.
我有五个从 Yahoo! 导入的股票投资组合。财务,需要创建一个包含所有股票 2016 年收盘价的 DataFrame。但是,我正在努力用相应的股票名称标记列。
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
回答by David Z
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.
但是,有一些方法可以使您的大块代码更加简洁,这还允许您将股票名称干净地指定为列名称。我会回到开头,(在定义start
和之后end
)首先创建一个你想要获取的股票行情列表。
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
从所有这些列中创建 a,然后DataFrame
直接从该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.
另一种方法是将所有股票数据放在 a 中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.
您会注意到它自动具有正确的列标签。
回答by Stephen Rauch
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'})