ComeçarComece de graça

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'.

Este exercício faz parte do curso

Importing and Managing Financial Data in Python

Ver curso

Instruções do exercício

  • 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 variable exchange that contains the name of each exchange. In each iteration:
    • Apply .value_counts() to 'Sector' and assign the result to sectors.
    • Sort sectors in descending order and plot them in a bar plot.
    • Show the result.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# 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()
Editar e executar o código