0

I'm trying to add a column to a dataframe

In yf_test.csv I have two columns:

original data

import pandas as pd
import yfinance as yf

ticker = pd.read_csv('yf_test.csv')
filtered = ticker['Filter1']
alist = []
for x in filtered:
    y = yf.Ticker(x)
    z = y.history(start=str(pnldate),end=str(pnldate))
    alist.append(z)

yf_data = pd.concat(alist)
price = yf_data['Close']
ticker['price']=price

What I wanted to do is to pull the prices from Yahoo Finance and then have them populate in a new column on dataframe ticker.

But when I do the above, I get an error:

ValueError: cannot reindex from a duplicate axis

What is the best way to achieve this?

Community
  • 1
  • 1
  • I think you just need to use a different name than "price" for the column - try "stock_price" or something like that. – mgrollins Nov 18 '19 at 23:33
  • See https://stackoverflow.com/questions/12555323/adding-new-column-to-existing-dataframe-in-python-pandas/35387129#35387129 – Alexander Nov 18 '19 at 23:35

0 Answers0