Technology IPOs by year on all exchanges
Each company in the listings
dictionary has an IPO year between 1972 and 2017. Therefore, in this context, it is appropriate to consider the 'IPO Year'
column of each sheet as a categorical variable with a well-defined order even though it is of dtype
float64
.
Here you will combine data from all three exchanges and plot the distribution of IPO years for companies in the Technology sector. pandas
as pd
and matplotlib.pyplot
as plt
have been imported, and the listings
dictionary from the previous exercise is in your workspace.
This exercise is part of the course
Importing and Managing Financial Data in Python
Exercise instructions
- Use a for loop with iterator variable
exchange
that contains the name of each exchange.- In each iteration, append the DataFrame corresponding to the key
exchange
inlistings
toall_listings
.
- In each iteration, append the DataFrame corresponding to the key
- After the loop completes, use
pd.concat()
to combine the three DataFrames inall_listings
and assign the result tolisting_data
. - Filter
listing_data
for'Technology'
companies and assign the result totech_companies
. - Assign the
'IPO Year'
column fromtech_companies
toipo years
. - For this data, use
.dropna()
to remove missing values and.astype()
to convert toint
. - Apply
.value_counts()
toipo_years
, sort the years in ascending order, and create a bar plot titled'Tech IPOs by Year'
. - Rotate
xticks
by 45 degrees and show the result.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create lists
exchanges = ['amex', 'nasdaq', 'nyse']
all_listings = []
# Use for loop to create listing_data
for exchange in exchanges:
all_listings.____(____[____])
# Combine DataFrames
listing_data = pd.____(____)
# Select tech companies
tech_companies = listing_data[____.____ == 'Technology']
# Create ipo_years
ipo_years = ____[____]
# Drop missing values and convert to int
ipo_years = ipo_years.____().____(int)
# Count values, sort ascending by year, and create a bar plot
ipo_years.____().plot(kind=____, ____='Tech IPOs by Year')
# Rotate xticks and show result
plt.xticks(____=____)
# Show the plot
plt.show()