1

I had the following question. Namely, suppose I have an excel file with some names in the first row. Then the next however many rows are single letter entries (so "A", "B", "C" and so on).

My goal is to extract the column and make it into a list, so that, say, for column 1 the list would start in row 2, the next entry would be from row 3, and so on, until the end is reached.

How would I do that in Python?

9
  • Your title sounds like a simple transpose which Excel can already do, but the question itself describes something I'm not clear on: the list for the first column would contain the whole column (excluding the header row), but the list for the last column would be missing the first row_length entries? Can you provide sample input and output? Commented May 24, 2016 at 2:36
  • @TigerhawkT3, no, every list would contain the entire column except for the first row. Commented May 24, 2016 at 2:38
  • So just transpose it in Excel? Commented May 24, 2016 at 2:47
  • @TigerhawkT3, I still need to convert it to the list, that's what I don't (or didn't) know. Commented May 24, 2016 at 2:53
  • 1
    @TigerhawkT3, OK, whatever, I answered my own question down below and at least Sonder's answer was helpful. Commented May 24, 2016 at 3:08

2 Answers 2

3

I used a module called xlrd.

Some information on that http://www.blog.pythonlibrary.org/2014/04/30/reading-excel-spreadsheets-with-python-and-xlrd/

And here is the package: https://pypi.python.org/pypi/xlrd

To exclude the first row, and make different lists for different columns you could do something like...

from xlrd import open_workbook

book = open_workbook("mydata.xlsx")
sheet = book.sheet_by_index(0) #If your data is on sheet 1

column1 = []
column2 = []
#...

for row in range(1, yourlastrow): #start from 1, to leave out row 0
    column1.append(sheet.cell(row, 0)) #extract from first col
    column2.append(sheet.cell(row, 1))
    #...

Put the index-1 of the last row that contains data in the 'yourlastrow' placeholder.

Sign up to request clarification or add additional context in comments.

2 Comments

The question was about transposing rows and columns, not about what libraries can read and write Excel files. This doesn't answer the question, and would be better suited as a comment. You will be able to leave a comment when you have sufficient reputation.
Thanks, that's all I needed.
2

I figured out the answer. Assuming the Excel file is Test.xlsx, we can translate the j-th column (excluding the first row) into list_j as follows:

book = xlrd.open_workbook("Test.xlsx")
sheet = book.sheet_by_index(0)

list_j = []

for k in range(1,sheet.nrows):
    list_j.append(str(sheet.row_values(k)[j-1]))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.