0

I'm trying to write a program where the user inputs multiple stocks tickers that will (eventually) get graphed.

import pandas as pd
import pandas_datareader.data as web
from datetime import datetime, timedelta

tickers = ['QQQ', 'SQQQ', 'RYCVX', 'RYCWX']
days = (datetime.now() - timedelta(5)).strftime('%Y-%m-%d')
data = {}

for t in tickers:
    data[t] = web.DataReader (t, 'yahoo', days, end = None)

Result:

{'QQQ':                   High         Low  ...    Volume   Adj Close
Date                                ...                      
2019-08-14  185.949997  182.419998  ...  51000500  182.759995
2019-08-15  183.589996  181.160004  ...  36685000  182.550003
2019-08-16  185.949997  184.100006  ...  32781400  185.479996
2019-08-19  188.830002  187.500000  ...  22494100  188.429993

[4 rows x 6 columns], 
'SQQQ':                  High        Low       Open      Close    Volume  Adj Close
Date                                                                       
2019-08-14  36.900002  35.000000  35.320000  36.730000  32427000  36.730000
2019-08-15  37.689999  36.230000  36.520000  36.849998  19184200  36.849998
2019-08-16  35.939999  34.830002  35.939999  35.110001  14002600  35.110001
2019-08-19  33.970001  33.220001  33.590000  33.529999  11745300  33.529999, 
'RYCVX':                  High        Low       Open      Close  Volume  Adj Close
Date                                                                     
2019-08-14  89.489998  89.489998  89.489998  89.489998       0  89.489998
2019-08-15  90.290001  90.290001  90.290001  90.290001       0  90.290001
2019-08-16  92.489998  92.489998  92.489998  92.489998       0  92.489998
2019-08-19  94.290001  94.290001  94.290001  94.290001       0  94.290001, 
'RYCWX':             High   Low  Open  Close  Volume  Adj Close
Date                                                  
2019-08-14  9.92  9.92  9.92   9.92       0       9.92
2019-08-15  9.83  9.83  9.83   9.83       0       9.83
2019-08-16  9.59  9.59  9.59   9.59       0       9.59
2019-08-19  9.40  9.40  9.40   9.40       0       9.40}

This creates a dictionary where tickers are keys and the stock data are the values. My original idea was to remove the key:value pair and assign to a variable but I have just learned my original idea was bad practice. What I now need to know how to do is the following:

  1. I need to be able to call a column value from a specific key such as the "High" column of the key QQQ. What I would normally do to call a column if it was assigned to a variable:
QQQ["High"]
  1. I need a loop to iterate a new column to every dataframe. What I would do if the df was assigned to a variable to create a new column:
QQQ['15MA'] = QQQ['Close'].rolling(15).mean()

How do I adapt the calling and iterating to a dictionary?

dancassin
  • 77
  • 1
  • 6
  • The `pop()` method removes the specified item from the dictionary and return the removed value – Shijith Aug 20 '19 at 05:33
  • Yes, that is the point. I want a function to remove the data from the dictionary by key and assign it to a variable – dancassin Aug 21 '19 at 00:44
  • dynamically creating variables is not considered a best practice. why do you wan to create a variable, why can't you use dictionary key as reference? Instead of removing items using pop, why can't you use get() method to get the value of a key? – Shijith Aug 21 '19 at 05:20

1 Answers1

3

First use function ouside loop for filling dictionary:

for t in tickers:
    data[t] = web.DataReader (t, 'yahoo', days, end = None)

datapop(t)

I think you need to change datapop for selecting by keys in []:

def datapop(ticker):
    return data[ticker]

Or by function get, which is more general, because possible set default value if key was not found:

def datapop(ticker):
    return data.get(ticker)

Reason is pop method not select, but is used for remove keys from dictionary.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • The for loop automatically adds the data to the dictionary. A separate function outside of the loop does not need to be used to fill the dictionary. The whole point of the "datapop" funciton is to pop the data from the dictionary and assign the pandas df to a variable. Please re-read my original post and let me know if you have any questions or if I need to re-write the question. – dancassin Aug 21 '19 at 03:16
  • 1
    @dancassin - But why? What is reason assign to variable? Because it is not [recommended](https://stackoverflow.com/a/30638956), better is use dictionary here for store DataFrames. – jezrael Aug 21 '19 at 05:15
  • 1
    Ah, thanks for the link. Incredibly helpful. Maybe I should completely rewrite my question since it is not only bad practice but apparently a duplicate. – dancassin Aug 22 '19 at 05:38
  • @dancassin - ya, it is up to you, but I think dict of DataFrames or one big DataFrame with `df_big = pd.concat(df)` is better here. – jezrael Aug 22 '19 at 05:40
  • Original question revised – dancassin Aug 22 '19 at 05:51