Creating a Pandas DataFrame from an Excel file

While many people will tell you to get data out of Excel as quickly as you can, Pandas provides a function to import data directly from Excel files. This saves you the time of converting the file.

How to do it…

  1. To create a Pandas DataFrame from an Excel file, first import the Python libraries that you need:
    import pandas as pd
  2. Next, define a variable for the accidents data file and enter the full path to the data file:
    customer_data_file = 'customer_data.xlsx'
  3. After that, create a DataFrame from the Excel file using the read_excel method provided by Pandas, as follows:
    customers = pd.read_excel(customer_data_file,
    sheetname=0,
    header=0,
    index_col=False,
    keep_default_na=True
    )
  4. Finally, use the head() command ...

Get Python Business Intelligence Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.