简体   繁体   中英

how to read sheets in an excel file with a for loop in python

how can I get these several dataframe below in a for loop

CTD4_100_ASV = pd.read_excel("verconnec.xlsx",sheet_name="100m-asv").set_index('sign')
CTD4_800_ASV = pd.read_excel("verconnec.xlsx",sheet_name="800m-asv").set_index('sign')
CTD4_1800_ASV = pd.read_excel("verconnec.xlsx",sheet_name="1800m-asv").set_index('sign')
CTD4_2800_ASV = pd.read_excel("verconnec.xlsx",sheet_name="2800m-asv").set_index('sign')
CTD4_3800_ASV = pd.read_excel("verconnec.xlsx",sheet_name="3800m-asv").set_index('sign')

I have tried this, but it doesn't work

  asvlist=["100m-asv","800m-asv","1800m-asv","2800m-asv","3800m-asv"]
  for i in range(len(asvlist)):
    CTD4_asvlist[i]=pd.read_excel("verconnec.xlsx",sheet_name=asvlist[i]).set_index('sign') 

you didn't define CTD4_asvlist . but there is a cleaner way to do it:

asvlist=["100m-asv","800m-asv","1800m-asv","2800m-asv","3800m-asv"]
CTD4_asvlist = [pd.read_excel("verconnec.xlsx",sheet_name=asv).set_index('sign') for asv in asvlist]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM