Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call different data files using a for loop in pandas?

I have a list of files named such as

topaccount_2015_09_individuals
topaccount_2015_12_indiviuuals
...
topaccount_2021_12_individuals

which are subsets of

topaccount_2015_09
topaccount_2015_12
...
topaccount_2021_12

I want to call them and do some data manipulation so i created a list,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

known_series = known['Address']
y = ['2015_09', '2015_12', '2016_03', '2016_06', '2016_09', '2016_12', '2017_03', '2017_06', '2017_09', '2017_12',
     '2018_03', '2018_06', '2018_09', '2018_12', '2019_03', '2019_06', '2019_09', '2019_12' , '2020_03', '2020_06', '2020_09', '2020_12', 
     '2021_03', '2021_03', '2021_06', '2021_09', '2021_12']

for q in y:
    topaccount_[q]_individuals = topaccount_[q][~topaccount_[q]['address'].isin(known_series)]
    topaccount_[q]_individuals = topaccount_[q]_individuals.reset_index(drop=True)

but it is giving me an error. what am I doing wrong? (known_series is already defined in the script)

>Solution :

Something like this then and use the name list later.

name = []
for x in y:
    name.append(f'topaccount_{x}_individuals') 
    
print(name)

['topaccount_2015_09_individuals', 'topaccount_2015_12_individuals', 'topaccount_2016_03_individuals', 'topaccount_2016_06_individuals', 'topaccount_2016_09_individuals', 'topaccount_2016_12_individuals', 'topaccount_2017_03_individuals', 'topaccount_2017_06_individuals', 'topaccount_2017_09_individuals', 'topaccount_2017_12_individuals', 'topaccount_2018_03_individuals', 'topaccount_2018_06_individuals', 'topaccount_2018_09_individuals', 'topaccount_2018_12_individuals', 'topaccount_2019_03_individuals', 'topaccount_2019_06_individuals', 'topaccount_2019_09_individuals', 'topaccount_2019_12_individuals', 'topaccount_2020_03_individuals', 'topaccount_2020_06_individuals', 'topaccount_2020_09_individuals', 'topaccount_2020_12_individuals', 'topaccount_2021_03_individuals', 'topaccount_2021_03_individuals', 'topaccount_2021_06_individuals', 'topaccount_2021_09_individuals', 'topaccount_2021_12_individuals']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading