0

I have an excel file which includes 5 sheet. I should create 5 graphs and plot them as x and y. but I should loop it. How can i do

1
  • @OP: you've asked a few questions since joining the site, some of which have one or more answers. Please consider upvoting and/or accepting those answers which have helped you. Commented Feb 26, 2022 at 9:28

2 Answers 2

1

You can load all the sheets:

f = pd.ExcelFile('users.xlsx')

Then extract sheet names:

>>> f.sheet_names
['User_info', 'purchase', 'compound', 'header_row5']

Now, you can loop over the sheet names above. For example one sheet:

>>> f.parse(sheet_name = 'User_info')
      User Name Country      City Gender  Age
0  Forrest Gump     USA  New York      M   50
1     Mary Jane  CANADA   Tornoto      F   30
2  Harry Porter      UK    London      M   20
3     Jean Grey   CHINA  Shanghai      F   30

The loop looks like this:

for name in f.sheet_names:
    df = f.parse(sheet_name = name)
    # do something here
Sign up to request clarification or add additional context in comments.

Comments

0

No need to use variables, create the output lists and use this simple loop:

data = pd.ExcelFile("DCB_200_new.xlsx")

l = ['DCB_200_9', 'DCB_200_15', 'DCB_200_23', 'DCB_200_26', 'DCB_200_28']

x = []
y = []
for e in l:
    x.append(pd.read_excel(data, e, usecols=[2], skiprows=[0,1]))
    y.append(pd.read_excel(data, e, usecols=[1], skiprows=[0,1]))

But, ideally you should be able to load the data only once and loop over the sheets/columns. Please update your question with more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.