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
.
Este exercício faz parte do curso
Importing and Managing Financial Data in Python
Instruções do exercício
- 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
.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 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.____([____, ____])