xx123
xx123

Reputation: 37

Adding a label column to a dataframe

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

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 ?

Below is the final output :

                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 : I am using iexfinance library to get historical prices.

Upvotes: 1

Views: 2593

Answers (1)

jpp
jpp

Reputation: 164623

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.

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)

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)

Upvotes: 2

Related Questions