I am scraping data from the web for multiple years (2007-2019). I want to output the data in different datasets called df_year, and have the year be the year of the data. In SAS, I could create a macro variable for the year, but not sure how to name datasets dynamically similarly in Python. Thanks!
Asked
Active
Viewed 193 times
-1
-
could you please elaborate on what you mean by "different datasets"? Are you trying to save the data in multiple files (e.g. CSV)? Or just keep multiple data frames in memory for analysis? – Roy2012 May 09 '20 at 18:10
-
sorry i mean multiple datasets in memory(i.e. df_2010, df_2011, etc.), I might choose to ultimately save them separately as a CSV or stack, but I want to leave that flexibility. – Mohini Vembu May 09 '20 at 19:23
1 Answers
0
Python doesn't have a notion of macros. The natural way to do what you're looking for is to use a dictionary:
df_by_year = {}
df_by_year[2011] = df
df_by_year[2012] = another_df
You can then access the data frame of a specific year by df_by_year[<my_year>]

Roy2012
- 11,755
- 2
- 22
- 35