2

stockticker_data is Pandas dataframe which contains a list of tickers but the for loop is not iterating over the complete list. For some reason it is stopping right after the first list symbol. What do I need to do for it to move on the the next symbol?

stockticker_data = pd.read_csv("Nifty 50 Scraped data.csv")

all_data = {}

for ticker in stockticker_data:
    all_data = web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019') 

print(all_data)

The output looks like this:

             High         Low  ...      Volume   Adj Close
Date                                ...                        
2018-01-02  172.300003  169.259995  ...  25555900.0  167.701889
2018-01-03  174.550003  171.960007  ...  29517900.0  167.672668
2018-01-04  173.470001  172.080002  ...  22434600.0  168.451508

I had modified the code to

for index, ticker in stockticker_data.iteritems():

    all_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019'))

print(all_data)

The output did change but still data was pulled for just 1 symbol : Is there something i am missing here ?

[Attributes   Adj Close                ...      Volume           
Symbols    AXISBANK.NS BAJAJ-AUTO.NS  ...  YESBANK.NS    ZEEL.NS
Date                                  ...                       
2018-01-01  564.798584   3152.436035  ...   4019878.0  1102118.0
2018-01-02  558.806030   3157.707031  ...   5224976.0   769766.0
2018-01-03  559.754822   3106.240723  ...   5672263.0  1207540.0
2018-01-04  559.205566   3128.667480  ...   5667580.0  1456032.0
2018-01-05  562.551331   3143.427002  ...  30720675.0  1568108.0
...                ...           ...  ...         ...        ...
2018-12-27  616.783997   2677.074463  ...  44303877.0  5809491.0
2018-12-28  624.274719   2658.001221  ...  27142501.0  6000271.0
2018-12-31  619.131104   2660.593262  ...  18970865.0  2663133.0
2019-01-01  626.521912   2666.950928  ...  24160878.0  2203533.0
2019-01-02  619.280884   2633.059570  ...  32583205.0  2687773.0

[248 rows x 288 columns]]

Process finished with exit code 0
Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • 2
    Does this answer your question? [How to iterate over rows in a DataFrame in Pandas?](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – Vishnudev Krishnadas Oct 29 '19 at 16:45
  • Also look at [this answer](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas/55557758#55557758) for alternatives to iterating that can accomplish what you want. – Code-Apprentice Oct 29 '19 at 16:48
  • "For some reason it is stopping right after the first list symbol." Why do you think that? Try moving your `print()` statement inside your list to see what happens. – Code-Apprentice Oct 29 '19 at 16:48
  • "The output looks like this:" What do you want the output to be instead? – Code-Apprentice Oct 29 '19 at 16:49
  • @Code-Apprentice I had tried moving the print statement inside the for-loop as you had suggested but the output did not change. I had modified the code to this: for index, ticker in stockticker_data.iteritems(): all_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019')) print(all_data) – Slartibartfast Oct 29 '19 at 17:18

3 Answers3

4

You are not actually adding to the all_data dictionary, you are only overwriting the variable so it will always be the last object in your for loop. Try appending to a list.

stockticker_data = pd.read_csv("Nifty 50 Scraped data.csv")

all_data = []

for ticker in stockticker_data:
    all_data.append(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019'))

print(all_data)
EcSync
  • 842
  • 1
  • 6
  • 20
  • I had added .append as you have suggested but the row count is still the same. which suggest that there is only data for 1 symbol. – Slartibartfast Oct 29 '19 at 17:11
  • @newcoder I'm not entirely sure...try `print(web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019')` inside of the `for` loop to see if the output changes – EcSync Oct 30 '19 at 06:45
0

Your print statement is outside of the loop, so it calculates all_data for each ticker, then prints all_data only for the last loop.

Also, looping through dataframes is inefficient. Better to do something like:

get_data = lambda ticker: web.get_data_yahoo(ticker, '1/1/2018', '1/1/2019')
stockticker_data['tickers'].apply(get_data)

where 'tickers' is the name of the column of stockticker_data containing the ticker symbols.

mermaldad
  • 322
  • 2
  • 7
0

Since you stated that you need to loop through the dataframe items, you can use iteritems method of dataframe object to get list of row indices and series object where column header can be used as attribute to get your value.

For example, lets say I have following data in "Nifty 50 Scraped data.csv"

Ticker
CSCO
DFS
SOMETHING

you can then get your item via following code,

for rowIndex,  col in stockticker_data.iterrows():
    print rowIndex, col.Ticker