Reading and writing Excel files with Python
Microsoft Excel files are arguably the most widely used format to exchange data in a tabular form. In the newest incarnation of the XLSX
format, Excel can store over one million rows and over 16 thousand columns in a single worksheet.
Getting ready
To execute this recipe, you will need the pandas
module installed. No other prerequisites are required.
How to do it…
The following is the code to read the Excel file. Note that we assume the pandas
module is already imported and aliased as pd
(the read_xlsx.py
file):
# name of files to read from and write to r_filenameXLSX = '../../Data/Chapter01/realEstate_trans.xlsx' w_filenameXLSX = '../../Data/Chapter01/realEstate_trans.xlsx' # open the Excel file xlsx_file = pd.ExcelFile(r_filenameXLSX) # read the contents xlsx_read = { sheetName: xlsx_file.parse(sheetName) for sheetName in xlsx_file.sheet_names } # print the first 10 prices for Sacramento print(xlsx_read['Sacramento'].head(10)['price']) # write to Excel xlsx_read['Sacramento'] \ .to_excel(w_filenameXLSX, 'Sacramento', index=False)
How it works…
We follow a similar manner to the previous examples. We first open the XLSX file and assign it to the xlsx_file
object using pandas
' ExcelFile(...)
method. We employ the .parse(...)
method to do the work for us and read the contents of the specified worksheet; we store it in the xlsx_read
dictionary. Note that you get access to all the worksheets in the Excel file through the .sheet_names
property of the ExcelFile
object.
To create the xlsx_read
dictionary, we use Pythonic dictionary comprehension: instead of looping through the sheets explicitly and then adding the elements to the dictionary, we use the dictionary comprehension to make the code more readable and compact.
Note
The comprehensions make it easy to understand the code as they mimic mathematical notations. Consider, for example, the following list of powers of 2: (A = (20, 21, 22, ..., 28) = (2x: 0 <= x < 9)
, x
is an integer). It can then easily be translated into Python using a list comprehension: A = [2**x for x in range(0, 9)]
. This would create the following list: A = [1, 2, 4, 8, 16, 32, 64, 128, 256]
.
Also, in Python, the comprehensions are also a tiny bit faster than explicit loops (http://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops).
The range(<from>,<to>)
command generates a sequence of integers starting at <from>
and extending to <to>
less one. For example, range(0,3)
will generate a sequence 0, 1, 2.
Storing the data in an Excel file is also very easy. All that is required is to invoke the .to_excel(...)
method, where the first parameter is the name of the file you want to save the data to and the second one specifies the name of the worksheet. In our example, we also specified the additional index=False
parameter that instructs the method not to save the index; by default, the .to_excel(...)
method saves the index in column A.
There's more…
Alternatively to reading Excel files using pandas
' read_excel(...)
, there are multiple Python modules you can use that provide Excel data reading capabilities. pandas
uses the xlrd
(https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966) module to read the data and then converts it to a DataFrame. For XLSX files, you can also use the openpyxl
module (the read_xlsx_alternative.py
file):
import openpyxl as oxl # name of files to read from r_filenameXLSX = '../../Data/Chapter01/realEstate_trans.xlsx' # open the Excel file xlsx_wb = oxl.load_workbook(filename=r_filenameXLSX) # names of all the sheets in the workbook sheets = xlsx_wb.get_sheet_names() # extract the 'Sacramento' worksheet xlsx_ws = xlsx_wb[sheets[0]]
We first read the contents of the Excel file and store it in xlsx_wb
(workbook). From the workbook, we extract the names of all the worksheets and put it in the sheets
variable. As we have only one worksheet in our workbook, the
sheets
variable equals to 'Sacramento'
. We use it to create an xlsx_ws
object that allows iterating through all the rows:
labels = [cell.value for cell in xlsx_ws.rows[0]] data = [] # list to hold the data for row in xlsx_ws.rows[1:]: data.append([cell.value for cell in row])
The first row contains the labels for all the columns so it is a good idea to store this separately—we put it in the labels
variable. We then iterate through all the rows in the worksheet, using the .rows
iterator, and append the values of all the cells to the data
list:
print( [item[labels.index('price')] for item in data[0:10]] )
The last part of the code prints out the prices of properties for the top 10 rows. We use list comprehension to create a list of the prices. You can find the first occurrence of a certain item in a list by calling .index(...)
on a list object, as we did in this example.
See also
Check the pandas
documentation for read_excel
at http://pandas.pydata.org/pandas-docs/stable/io.html#io-excel. Also, you can visit http://www.python-excel.org for a list of modules that allow you to work with data stored in different Excel formats, both older .xls
and newer .xlsx
files.