Search code examples
pythonz-index

How to combine a bitcoin dataset and google into one dataframe as following?


Would you please tell me how to correct the following? When I run it, the error appears.

ValueError: Index contains duplicate entries, cannot reshape

import datetime
import pandas_datareader.data as web
# set start and end dates
start = datetime.datetime(2018, 2, 15)
end = datetime.datetime(2020, 2, 14)
# extract the closing price data
combined_df = web.DataReader(["BTC-USD","ETH-USD","GOOG"],
"yahoo", start = start, end = end)

Solution

  • import datetime
    import pandas as pd
    import numpy as np
    import yfinance as yf
    # pip install yfinance
    # updated version of yahoo_datareader
    
    start = datetime.datetime(2018, 2, 15)
    end = datetime.datetime(2020, 2, 14)
    symbols = ['BTC-USD', 'ETH-USD', 'GOOG']
    
    df = pd.DataFrame()
    
    for i in symbols:
        data = yf.download(i, start, end)
        df[i] = data['Adj Close']
    
    
    df = df.fillna(method='ffill')
    
    ind = list(df.index)
    
    df.index = list(range(len(df.index)))
    for i in df.index: 
        if df['GOOG'].iloc[i] == 0: 
            df['GOOG'].iloc[i] = df['GOOG'].iloc[i-1]
    
    changes = []
    for i in df.index:
        if i == 0:
            changes.append(0.)
        else:
            current = df['GOOG'].iloc[i]
            previous = df['GOOG'].iloc[i-1]
            perc_change = np.round(((current - previous) / previous)*100),2)
            changes.append(perc_change)
    
    df['% Change'] = changes
    
    df.index = ind