Repository navigation
Expand file tree
/
Copy pathspotify.py
More file actions
206 lines (104 loc) · 3.89 KB
/
Copy pathspotify.py
File metadata and controls
206 lines (104 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# In[2]:
sp_tracks = pd.read_csv('D:/spotifydata/tracks.csv')
sp_feature = pd.read_csv('D:/spotifydata/SpotifyFeatures.csv')
# In[3]:
#viewing the tracks data
sp_tracks.head()
# In[4]:
#viewing the feature data
sp_feature.head()
# In[5]:
#checking null
pd.isnull(sp_tracks).sum()
# In[6]:
pd.isnull(sp_feature).sum()
# In[7]:
#checking info
sp_tracks.info()
# In[8]:
#checking info
sp_feature.info()
# In[9]:
#finding 10 least popular songs in the spotify dataset
a=sp_tracks.sort_values('popularity',ascending=True)[0:10]
a[['name','popularity']]
# In[10]:
#descriptive statistics of tracks
sp_tracks.describe().transpose()
# In[11]:
#descriptive of feature
sp_feature.describe().transpose()
# In[12]:
#finding top 10 popular songs in the spotify dataset
a=sp_tracks
b=a[a['popularity']>90].sort_values('popularity',ascending=False)[:10]
b[['name','popularity','artists']]
# In[13]:
#Make the Release Date Column as the Index Column.
sp_tracks.set_index('release_date',inplace=True)
sp_tracks.index=pd.to_datetime(sp_tracks.index)
sp_tracks.head()
# In[14]:
#Find the Name of the Artist Present in the 18th Row of the Dataset.
sp_tracks[['artists']].iloc[18]
# In[15]:
#Convert the Duration of the Songs From Milliseconds to Seconds.
sp_tracks['duration'] = sp_tracks['duration_ms'].apply (lambda x : round(x/1000))
sp_tracks.drop('duration_ms', inplace = True, axis=1)
sp_tracks.duration.head()
# In[16]:
#Correlation HeatMap using Pearson Correlation method between two variables
td = sp_tracks.drop(['key','mode','explicit'], axis=1).corr(method = 'pearson')
plt.figure(figsize=(9,5))
hmap = sns.heatmap(td, annot = True, fmt = '.1g', vmin=-1, vmax=1, center=0, cmap='Greens', linewidths=0.1, linecolor='black')
hmap.set_title('Correlation HeatMap')
hmap.set_xticklabels(hmap.get_xticklabels(), rotation=90)
# In[17]:
#Sample Only 4 Percent of the Whole Dataset.
sample_sp=sp_tracks.sample(int(0.004*len(sp_tracks)))
print(len(sample_sp))
# In[18]:
#Create a Regression Plot Between Loudness and Energy. Let’s Plot It in the Form of a Regression Line.
plt.figure(figsize=(8,4))
sns.regplot(data=sample_sp, y='loudness', x='energy', color='#054907').set(title='Regression Plot - Loudness vs Energy Correlation')
# In[19]:
#Create a Regression Plot Between Popularity and Acousticness in the Form of a Regression Line.
plt.figure(figsize=(8,4))
sns.regplot(data=sample_sp, y='popularity', x='acousticness', color='#008000').set(title='Regression Plot - Popularity vs Acousticness Correlation')
# In[20]:
#creating new column in tracks table
sp_tracks['dates']=sp_tracks.index.get_level_values('release_date')
sp_tracks.dates=pd.to_datetime(sp_tracks.dates)
years=sp_tracks.dates.dt.year
sp_tracks.head()
# In[21]:
sns.displot(years, discrete=True, aspect=2, height=4, kind='hist',color='g').set(title='No of songs - per year')
# In[22]:
#Plot a Line Graph to Show the Duration of the Songs for Each Year.
total_dr = sp_tracks.duration
fig_dims = (15,5)
fig, ax = plt.subplots(figsize=fig_dims)
fig = sns.barplot(x = years, y = total_dr, ax = ax, errwidth = False).set(title='Years vs Duration')
plt.xticks(rotation=90)
# In[27]:
#spotify feature analysis
#Plot Duration of the Songs w.r.t. different Genres using a horizontal barplot.
plt.title('Duration of songs in different Genres')
sns.color_palette('crest', as_cmap=True)
sns.barplot(y='genre', x='duration_ms', data=sp_feature)
plt.xlabel('Duration in ms')
plt.ylabel('Genres')
# In[33]:
#Find top five genres by Popularity and pot a barplot for the same.
sns.set_style(style='darkgrid')
plt.figure(figsize=(8,4))
Top = sp_feature.sort_values('popularity', ascending=False)[:10]
sns.barplot(y = 'genre', x = 'popularity', data = Top).set(title='Genres by Popularity-Top 5')
# In[ ]: