Search code examples
pythonexcelfor-loopxlrd

How do you export data from Excel into Python using For Loops?


I have figure out how to print the data from an Excel spreadsheet using a For Loop, but know I would like to export each column as a different variable so I can manipulate them, for example plot a graph using plot.ly

What I have used so far is;

import xlrd
book = xlrd.open_workbook('filelocation/file.xlsx')
sheet = book.sheet_by_index(0)

for j in range(1,4): 
    for i in range(2,8785): 
        print "%d" %sheet.cell_value(i,j)

Which just prints all the numbers from the spreadsheet into my terminal which is not that useful.

But I would like something like this;

import xlrd
book = xlrd.open_workbook('filelocation/file.xlsx')
sheet = book.sheet_by_index(0)

for j= 1: 
    for i in range(2,8785): 
        Time= "%s" %sheet.cell_value(i,j)

for j= 2:
    for i in range(2,8785): 
        SYS= "%s" %sheet.cell_value(i,j)        

which would declare different variables for each column. But as I understand from the error message I seem to be using the For Loops wrong, I am not that familiar with For Loops in Python, I have only really used them in Matlab.

* EDIT * fixed indentation in the question, was fine in the original code, not the source of error.


Solution

  • I like pandas for all this sort of thing.

    you can create a DataFrame object which will hold all the data you're looking for:

    import pandas as pd
    
    df = pd.read_excel('myfile.xlsx', sheetname='Sheet1')
    

    now you can access each column by its name out of that dataframe, so if you had a column called 'mynumbers' (idk) you would get it py doing:

    print df['mynumbers']
    

    or you could iterate over all columns using:

    for col in df.columns:
        print df[col]
    

    then you can do whatever you like, including some built-in plotting, visualisation and stats if you have a look around the docs.