Please read the pinned thread to see how to format code for the forum properly, so that we can see the indentation of the code properly and understand its structure.
That said, I guess that you are trying to use pandas.to_csv
multiple times for the same file. This will not work - it does not care about what is already in the file.
I’m not sure why you would want to put data about multiple stocks in the same file. It won’t make sense if you have the rows of data for A2A.MI
and then new rows of data for AMP.MI
etc. - that’s not how a CSV file works. You won’t know where the data ends for one stock and begins for the next stock, and would have to search through the data for a new header or something like that.
If you really do want one file, you can tell pandas.to_csv
to append instead of overwriting, using mode='a'
(the same way that you would if you were open
ing a file directly yourself). If you don’t want the extra headers, use header=False
- I guess you want it every time except the first (which is also a little tricky).
Unless your data is really huge (some GB or so), you will be better off if you use Pandas to append (and filter) all the data in memory first, and only write one file after you have all the data ready. This will also be easier, if you want the stock data side by side (a new group of columns for each stock). (I say “easier”, but the other way is so difficult that I wouldn’t bother to try.) You can use pandas.concat
to combine the rows, either way (in separate columns, or in more rows in the same columns).