import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/ProgramData/Anaconda3/Library/plugins/platforms'

import pandas as pd 
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings("ignore")
#path to our csv file
file_path = r"\\apporto.com\dfs\LOYOLA\Users\hmhodges_loyola\Desktop\IS 460 Data\Popular_Spotify_Songs.csv"

#used this for the specific encoding for the csv file
df = pd.read_csv(file_path, encoding='latin1')

Do you know any of these songs?

Introduction

Welcome to Top Spotify Songs in Jupyter Notebook. This data set has 24 columns and 953 rows! There are close to 1000 songs in this data set that are all ranked based on popularity, danceabilty, etc. This data set also provides the artist(s) on the track, when it was was released, and its overall rank in the chart. We are going to take a deeper look into the data in this data set to say if there is a specific pattern in the top ranking songs, how many artists produce the most popular songs, and honestly if we know any of these popular songs. Let’s first take a look at some stats and our data set. Then we will go into our analysis.

Dataset

df.head()
##                             track_name  ... speechiness_%
## 0  Seven (feat. Latto) (Explicit Ver.)  ...             4
## 1                                 LALA  ...             4
## 2                              vampire  ...             6
## 3                         Cruel Summer  ...            15
## 4                       WHERE SHE GOES  ...             6
## 
## [5 rows x 24 columns]

Stats

stats = df.describe

print(stats)
## <bound method NDFrame.describe of                               track_name  ... speechiness_%
## 0    Seven (feat. Latto) (Explicit Ver.)  ...             4
## 1                                   LALA  ...             4
## 2                                vampire  ...             6
## 3                           Cruel Summer  ...            15
## 4                         WHERE SHE GOES  ...             6
## ..                                   ...  ...           ...
## 948                         My Mind & Me  ...             3
## 949            Bigger Than The Whole Sky  ...             6
## 950                 A Veces (feat. Feid)  ...             6
## 951                        En La De Ella  ...             5
## 952                                Alone  ...             5
## 
## [953 rows x 24 columns]>

Analysis

Artists

First we are going to look at artists in the data set that have more than 3 songs.

#Counting and finding the artists with more than 3 songs in the data set
artist_counts = df['artist(s)_name'].value_counts()
artist_counts_filtered = artist_counts[artist_counts > 3]

#Creating a bar chart
plt.figure(figsize=(10, 6))
artist_counts_filtered.plot(kind='bar')
plt.title('Artists with More Than 3 Songs')
plt.xlabel('Artist')
plt.ylabel('Number of Songs')
plt.xticks(rotation=45, ha='right') 
plt.tight_layout()
plt.show()

As we can see Taylor Swift comes out on top with a huge lead over the other artists with almost 35 most popular songs. In my opinion, I can not say I am surprised; she is a global sensation… or at least some might say. There are 25 artists in this data set with at least 3 songs. It is impressive that out of almost 1000 songs these artists appear at least 3 times.

Pattern 1

Now, we are going to start to see if there is a pattern in making a most popular songs. First we are going to look at which years produced the most popular songs.

released_years = df['released_year']
year_counts = released_years.value_counts()

#Data for bubble chart
years = year_counts.index
counts = year_counts.values
sizes = counts * 10  

#Creating bubble chart
plt.figure(figsize=(10, 6))
plt.scatter(years, [1] * len(years), s=sizes, alpha=0.5)

plt.xlabel('Released Year')
plt.ylabel('Count')
plt.title('Bubble Chart of Released Years')
plt.grid(True)

#Showing the chart
plt.show()

Looking at this bubble chart, we can clearly see that from this data set, a majority of the most popular songs were produced in the most recent years (2019-2023). There could be many explanations for this. One that quickly comes to mind is the fact that technology changed overtime, which could have helped songs sound better. Also with the rise of social media, many artists are able to reach a larger audience which may contribute to the fact that more popular songs were produced more recently.

Pattern 2

To get more specific we are going to look at the months that the most popular songs were released.

#Count of each month
month_counts = df['released_month'].value_counts()

#Labeling each month in the pie chart
#Months in the dataset are numbered 1-12 so added names 
month_names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

#Creating a pie chart
plt.figure(figsize=(8, 8))
plt.pie(month_counts, labels=month_names, autopct=lambda p: '{:.0f}'.format(p * sum(month_counts) / 100), startangle=140)
plt.title('Count of Songs Released by Month')
plt.axis('equal')  
#Added up the number of songs in total
total_songs = sum(month_counts)
plt.text(0, -1.2, f'Total Songs: {total_songs}', ha='center', fontsize=12)
plt.show()

Here we are a pie chart, split into 12 chuncks for the 12 months of the year. As you can see January comes out on top when it comes to producing the most popular songs with Feburary as a close second. March and April come slighty behind. From this pie chart, with the data in this dataset we can conclude that the first few months of the year seem to be the time that the most popular songs are produced.

Audio Features

Now we are going to look at the specifics of the most popular songs. First we are going to look at how easy it is to understand/sing these songs.

#Pulling the specific data 
speechiness_data = df['speechiness_%']

#Creating the boxplot
plt.figure(figsize=(8, 6))
plt.boxplot(speechiness_data)
plt.title('Boxplot of Speechiness')
plt.ylabel('Speechiness (%)')
plt.show()

Here, we have a boxplot of speechiness (by percentage). This boxplot has serveral outliers. This indicates that there are many songs in the data set that are easy to sing, but the average is quite low. This is interesting because this could indicate that maybe many songs are not popular for their lyrics, but for how they sound.

Audio Features 2

Lastly, we are going to look at 4 specifics of these popular songs.

#Choosing what data we want in our chart
#Taking the average of all of them 
speechiness_percentage = df['speechiness_%'].mean()
liveness_percentage = df['liveness_%'].mean()
danceability_percentage = df['danceability_%'].mean()
energy_percentage = df['energy_%'].mean()

features = ['Speechiness', 'Liveness', 'Danceability', 'Energy']
percentages = [speechiness_percentage, liveness_percentage, danceability_percentage, energy_percentage]
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']

#Create donut chart
plt.figure(figsize=(8, 6))
plt.pie(percentages, labels=features, colors=colors, autopct='%1.1f%%', startangle=90, wedgeprops=dict(width=0.3))
#Draw a circle in the center to make it a donut chart
centre_circle = plt.Circle((0,0),0.70,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

#Equal aspect ratio ensures that pie is drawn as a circle
plt.axis('equal')
plt.title('Audio Features Distribution')
plt.show()

Using a donut chart, we are looking at all of the songs average danceabilty, energy, liveness, and speechiness (all by percentage). Surprisingly, the average speechiness is sitting at around 6% average. That does however confirm what we found in the boxplot. Danceabilty and Engery have the highest averages. This overall confirms that these most popular songs have more to do with how they sound rather than how easy they are to sing.

Conclusion

After looking through the data set and the visuals produced, we can conclude a few things. For one, Taylor Swift has the most popular songs in this data set. Whether you like her or not, you can’t deny she makes songs that a lot of people like. Two, from the pie chart and box plot, we can conclude that a majority of the most popular songs were produced in the first 4 months of the calendar year. While we can not conclude a specific pattern, we can say the most popular songs were in this data set were produced in the most recent years, and are released in the first 4 months of the year. Lastly, we can conclude, that the most popular songs in this data set are not ones that are easy to sing. They are very easy to dance to and have a lot of energy. Which concludes that if a song sounds good and you can dance to it, it could be a very popular song, even if you can’t sing it very well.