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'.
Cet exercice fait partie du cours
Importing and Managing Financial Data in Python
Instructions
- Create a list
exchangescontaining the exact strings of the names of the exchanges in the order listed above. - Use a for loop to iterate over
exchangeswith an iterator variableexchangethat contains the name of each exchange. In each iteration:- Apply
.value_counts()to'Sector'and assign the result tosectors. - Sort
sectorsin descending order and plot them in a bar plot. - Show the result.
- Apply
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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()