Book Image

Pandas 1.x Cookbook - Second Edition

By : Matt Harrison, Theodore Petrou
Book Image

Pandas 1.x Cookbook - Second Edition

By: Matt Harrison, Theodore Petrou

Overview of this book

The pandas library is massive, and it's common for frequent users to be unaware of many of its more impressive features. The official pandas documentation, while thorough, does not contain many useful examples of how to piece together multiple commands as one would do during an actual analysis. This book guides you, as if you were looking over the shoulder of an expert, through situations that you are highly likely to encounter. This new updated and revised edition provides you with unique, idiomatic, and fun recipes for both fundamental and advanced data manipulation tasks with pandas. Some recipes focus on achieving a deeper understanding of basic principles, or comparing and contrasting two similar operations. Other recipes will dive deep into a particular dataset, uncovering new and unexpected insights along the way. Many advanced recipes combine several different features across the pandas library to generate results.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

Using Excel files

While CSV files are common, it seems that the world is ruled by Excel. I've been surprised in my consulting work to see how many companies are using Excel as a critical if not the critical tool for making decisions.

In this recipe, we will show how to create and read Excel files. You may need to install xlwt or openpyxl to write XLS or XLSX files, respectively.

How to do it...

  1. Create an Excel file using the .to_excel method. You can write either xls files or xlsx files:
    >>> beatles.to_excel("beat.xls")
    >>> beatles.to_excel("beat.xlsx")
    
    Excel file

    Excel file

  2. Read the Excel file with the read_excel function:
    >>> beat2 = pd.read_excel("/tmp/beat.xls")
    >>> beat2
       Unnamed: 0    first       last  birth
    0           0     Paul  McCartney   1942
    1           1     John     Lennon   1940
    2           2  Richard    Starkey   1940
    3           3 ...