Companies by sector on all exchanges
A categorical variable is a variable that is one of a limited number of values based on some qualitative property. A frequency distribution is a representation of the number of times that a categorical variable occurs.
Think back to the stock exchange data from the earlier chapters. The .mean()
function isn't very helpful for understanding the frequency of 'Sector'
values such as 'Technology'
and 'Finance'
, whereas .value_counts()
and .nunique()
are.
In this exercise, you will compare the distribution of listings in the AMEX, NASDAQ, and NYSE per sector. pandas
as pd
and matplotlib.pyplot
as plt
have been imported, and the listings information from previous exercises has been loaded into a dictionary listings
for which the keys are 'amex'
, 'nasdaq'
, and 'nyse'
.
This exercise is part of the course
Importing and Managing Financial Data in Python
Exercise instructions
- Create a list
exchanges
containing the exact strings of the names of the exchanges in the order listed above. - Use a for loop to iterate over
exchanges
with an iterator variableexchange
that contains the name of each exchange. In each iteration:- Apply
.value_counts()
to'Sector'
and assign the result tosectors
. - Sort
sectors
in descending order and plot them in a bar plot. - Show the result.
- Apply
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create the list exchanges
exchanges = [____, ____, ____]
# Iterate over exchanges then plot and show result
for ____ in exchanges:
sectors = listings[____].____.____()
# Sort in descending order and plot
sectors.sort_values(____=____).plot(____=____)
# Show the plot
plt.show()