将标签列添加到数据框 [英] Adding a label column to a dataframe

查看:56
本文介绍了将标签列添加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中获取股票数据(OHLC)和日期作为索引.但是,股票代号名称未在此处显示. 因此,数据看起来像这样:

I have a data frame where I get the stocks data (OHLC) and date as index. However, the ticker name is not shown there. So, data looks something like this :

                open      high       low     close     volume
date                                                         
2013-10-11   63.7003   64.5963   63.4609   64.4619   66934938
2013-10-14   64.0718   65.0855   64.0090   64.8841   65474542
2013-10-15   65.0757   65.6637   64.8161   65.2294   80018603
2013-10-16   65.5054   65.7330   65.3014   65.5478   62775013
2013-10-17   65.3995   66.0273   65.3602   65.9907   63398335
2013-10-18   66.1856   66.6133   66.1490   66.5649   72635570

我有一个股票代码的列表,并且我正在运行一个for循环以获取相同的代码,然后使用concat/append最终获取数据.但是,我想在此处添加股票代码.我该怎么做 ?

I have a list of ticker symbols and I am running a for loop to get the same and then using concat/append to finally get the data. However, I want to add ticker symbols here. How can I do it ?

以下是最终输出:

                open      high       low     close     volume   ticker
date                                                         
2013-10-11   63.7003   64.5963   63.4609   64.4619   66934938   AAPL
2013-10-14   64.0718   65.0855   64.0090   64.8841   65474542   AAPL
2013-10-15   65.0757   65.6637   64.8161   65.2294   80018603   AAPL
2013-10-16   65.5054   65.7330   65.3014   65.5478   62775013   AAPL
2013-10-17   65.3995   66.0273   65.3602   65.9907   63398335   AAPL
.................
.................
.................
.................
2013-10-11  153.0422  154.3197  152.9154  154.2654  104967037   SPY
2013-10-14  153.3140  155.0083  153.1962  154.8815  111901876   SPY
2013-10-15  154.4919  155.0718  153.5496  153.7580  153958055   SPY
2013-10-16  154.6822  155.9869  154.6051  155.9053  161058684   SPY
2013-10-17  155.2711  157.0379  155.2439  156.9473  129004482   SPY

PS:我正在使用iexfinance库获取历史价格.

PS : I am using iexfinance library to get historical prices.

推荐答案

我对iexfinance库不熟悉.但是,假设您有一个神奇的函数get_data_from_ticker,顾名思义,该函数通过代码输入获得数据,可能是作为pd.DataFrame对象.

I'm not familiar with the iexfinance library. But Let's say you have a magic function get_data_from_ticker which, as the name implies, gets data given a ticker input, possibly as a pd.DataFrame object.

给出列表tickers,您当前的过程可能如下所示:

Given a list tickers, your current process may look like:

dfs = []
for ticker in tickers:
    data = get_data_from_ticker(ticker)
    dfs.append(data)
df = pd.concat(dfs)

如果代码信息未存储在您的数据框中,则此功能不是特别有用.因此,您可以使用 pd.DataFrame.assign 来相应地添加一系列:

This isn't particularly useful if the ticker information is not stored in your dataframe. So you can use pd.DataFrame.assign to add a series accordingly:

dfs = []
for ticker in tickers:
    data = get_data_from_ticker(ticker)
    dfs.append(data.assign(ticker=ticker))
df = pd.concat(dfs)

最后,您可以使用列表理解来提高效率:

Finally, you can make this a more efficient by using a list comprehension:

dfs = [get_data_from_ticker(ticker).assign(ticker=ticker) for ticker in tickers]

df = pd.concat(dfs)

这篇关于将标签列添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆