Load all listing data and iterate over key-value dictionary pairs
You already know that a pd.DataFrame()
object is a two-dimensional labeled data structure. As you saw in the video, the pd.concat()
function is used to concatenate, or vertically combine, two or more DataFrames. You can also use broadcasting to add new columns to DataFrames.
In this exercise, you will practice using this new pandas
function with the data from the NYSE and NASDAQ exchanges. pandas
has been imported as pd
.
Cet exercice fait partie du cours
Importing and Managing Financial Data in Python
Instructions
- Import data in
listings.xlsx
from sheets'nyse'
and'nasdaq'
into the variablesnyse
andnasdaq
. Read'n/a'
to represent missing values. - Inspect the contents of both DataFrames with
.info()
to find out how many companies are reported. - With broadcasting, create a new reference column called
'Exchange'
holding the values'NYSE'
or'NASDAQ'
for each DataFrame. - Use
pd.concat()
to concatenate thenyse
andnasdaq
DataFrames, in that order, and assign tocombined_listings
.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Import the NYSE and NASDAQ listings
nyse = pd.____('listings.xlsx', ____='nyse', na_values='n/a')
nasdaq = pd.____('listings.xlsx', ____='nasdaq', na_values='n/a')
# Inspect nyse and nasdaq
nyse.____()
nasdaq.____()
# Add Exchange reference columns
nyse['____'] = 'NYSE'
nasdaq['____'] = 'NASDAQ'
# Concatenate DataFrames
combined_listings = pd.____([____, ____])