Python Tutorial: Read data from Excel worksheets
Key Takeaways
This video tutorial demonstrates how to import data from Excel worksheets into Python using the pandas library, specifically the read_excel function. It covers how to import single or multiple sheets, handle missing values, and retrieve sheet names from an Excel workbook.
Full Transcript
let us now look at how to import data from Excel worksheets as example you will be using an Excel workbook with three worksheets containing listing information for three exchanges the AMEX Exchange and the Nasdaq that you're already familiar with and also the NYC each sheet contains the same information as the AMEX csv file you have seen before we just omitted the last update color you can use the parameter sheet name to tell read Excel which worksheet to import you have several options to import either a single sheet or multiple sheets simultaneously first you can provide an integer that refers to the position of the worksheet the number 0 means you want to import the first sheet you can also refer to a sheet by its name second you can import several sheets at the same time just provide a list with the names of positions of the sheets you would like read Excel to import the result will be a dictionary where the keys are the sheet names and the values are data frames with the sheet content let's look at an example to read the worksheet for the AMEX exchange simply provide the label to the read Excel parameter sheet name note that read Excel also uses the NA values parameter to parse missing values if you call the dot info method on the result you will notice the same output you obtained earlier from the read CSV method there's no import data from two worksheets just supply a list with the labels MX and a stack to the sheet name parameter the result contained in the variable listings is a dictionary that contains two key value pairs the keys contain the names of the worksheets and the values are the corresponding data frames since listings is a dictionary you can access the data frame within a stock data by providing the matching key once you apply the right info method to the result you can view the structure of the data about the listings on the stock exchange pandas also allows you to retrieve the sheet names from an Excel workbook to obtain this information create an excel file object using the path to an excel workbook as illustrated here for the listings xlsx file once you have created this object you can access its sheet names attribute this attribute contains a list with the names of the worksheets for this workbook here we retrieve the list of all the exchange names and assign it to a variable exchanges in the next step you can pause the excel file object to read Excel to import its content instead of the path to the file we can then select the name of the target worksheet from the list stored in the exchanges variable assign the resulting data frame to the variable NYC and call the method or info on this data frame to show the expected output let's now practice your new skills
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/importing-and-managing-financial-data-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Let us now look at how to import data from Excel worksheets.
As example, you will be using an Excel workbook with 3 worksheets containing listing information for 3 exchanges: the AmEx Exchange and NASDAQ that you are already familiar with, and also the NYSE. Each sheet contains the same information as the AMEX csv file you have seen before, we just omitted the ‘Last Update’ column.
You can use the parameter sheet_name to tell read_excel which worksheet to import. You have several options to import either a single sheet, or multiple sheets simultaneously:
you can provide an integer that refers to the position of the worksheet. The number 0 means you want to import the first sheet. You can also refer to a sheet by its name.
You can import several sheets at the same time. Just provide a list with the names or positions of the sheets you would like read_excel to import. The result will be a dictionary, where the keys are the sheet names, and the values are DataFrames with the sheet content.
Let's look at an example. To read the worksheet for the AmEx exchange, simply provide the label to the read_excel() parameter sheet_name. Note that read_excel() also uses the na_values parameter to parse missing values. If you call the .info() method on the result, you will notice the same output you obtained earlier from the read_csv() method.
Let's now import data from two worksheets. Just supply a list with the labels ‘amex’ and ‘nasdaq’ to the sheet_name parameter. The result contained in the variable ‘listings’ is a dictionary that contains two key-value pairs. The keys contain the names of the worksheets, and the values are the corresponding DataFrames.
Since listings is a dictionary, you can access the DataFrame with the NASDAQ data by
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
🎓
Tutor Explanation
DeepCamp AI