Lab 10
Lab 10
Lab Guide 10
3. Using the data from the file music.xlsx, create a Python script (Lab10_yourname.py) that does the
following:
a. Import the data in the file into a DataFrame, songs.
b. Sort the DataFrame by danceability.
c. Add a new column to songs, duration_sec, which stores the duration of each song in seconds (hint:
the duration column stores duration in milliseconds. There are 1000 milliseconds in a second).
d. Select the records with the music_genre of Rock and store as a new DataFrame, rock.
e. Select the records with the music_genre of Hip-Hop and store as a new DataFrame, hh.
f. Select the records with the music_genre of Alternative and store as a new DataFrame,
alternative.
g. Open a new Figure1 window and create the pie and bar charts shown in the figure below.
i. The pie chart compares the total number of Hip-Hop songs, Rock songs and Alternative
songs. The colors used are purple, red, and brown.
ii. The bar charts show the average (mean) popularity of Hip-Hop and Rock compared to the
mean popularity of all music_genres.
h. Create the plots shown in Figure2 below. The plots compare ‘danceability’ vs. ‘energy’ for
Alternative and Hip-Hop music.
i. Create the histogram in the same Figure window which shows the distribution of the song durations
in seconds.
Hint: to plot the histogram, the hist function calculates the number of values in each bin, and the
lower bound of each bin. It returns these values in a tuple, where the second element in the tuple is
the lower bound of each bin. You can save the return values and set the ticks on the axis using this
value:
hist_data = plt.hist( … )
plt.xticks( hist_data[1] )