{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.4-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"source": [
"# Listening Analysis\n",
"\n",
"Combining Spotify & Last.fm data for exploring habits and trends\n",
"Uses two data sources,\n",
"\n",
"1. Last.fm scrobbles\n",
"2. Spotify audio features\n",
"\n",
"The two are joined by searching Last.fm tracks on Spotify to get a Uri, the track name and artist name are provided for the query.\n",
"These Uris can be used to retrieve Spotify feature descriptors. `all_joined()` gets a BigQuery of that joins the scrobble time series with their audio features and provides this as a panda frame."
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"track object\n",
"album object\n",
"artist object\n",
"time object\n",
"uri object\n",
"acousticness float64\n",
"danceability float64\n",
"duration_ms int64\n",
"energy float64\n",
"instrumentalness float64\n",
"key int64\n",
"liveness float64\n",
"loudness float64\n",
"mode int64\n",
"speechiness float64\n",
"tempo float64\n",
"time_signature int64\n",
"valence float64\n",
"dtype: object"
]
},
"metadata": {},
"execution_count": 14
}
],
"source": [
"scrobbles.dtypes"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": []
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" acousticness danceability duration_ms energy instrumentalness \\\n",
"mean 0.170649 0.589141 2.422924e+05 0.711968 0.213591 \n",
"std 0.246679 0.173905 1.220714e+05 0.204289 0.335353 \n",
"min 0.000000 0.000000 1.578700e+04 0.000000 0.000000 \n",
"25% 0.004320 0.470000 1.893220e+05 0.586000 0.000000 \n",
"50% 0.045500 0.599000 2.264410e+05 0.749000 0.001100 \n",
"75% 0.237000 0.724000 2.787440e+05 0.878000 0.394000 \n",
"max 0.996000 0.981000 4.995315e+06 0.999000 0.995000 \n",
"\n",
" key liveness loudness mode speechiness tempo \\\n",
"mean 5.328584 0.216903 -7.127309 0.581856 0.146982 124.640429 \n",
"std 3.673929 0.173524 3.646891 0.493257 0.136440 30.809049 \n",
"min 0.000000 0.000000 -60.000000 0.000000 0.000000 0.000000 \n",
"25% 2.000000 0.099900 -8.590000 0.000000 0.047500 97.805000 \n",
"50% 6.000000 0.141000 -6.472000 1.000000 0.080800 124.992000 \n",
"75% 9.000000 0.300000 -4.827000 1.000000 0.223000 143.188000 \n",
"max 11.000000 0.995000 3.108000 1.000000 0.966000 248.028000 \n",
"\n",
" time_signature valence \n",
"mean 3.957806 0.418024 \n",
"std 0.356726 0.236941 \n",
"min 0.000000 0.000000 \n",
"25% 4.000000 0.221000 \n",
"50% 4.000000 0.398000 \n",
"75% 4.000000 0.597000 \n",
"max 5.000000 0.983000 "
],
"text/html": "
\n\n
\n \n \n | \n acousticness | \n danceability | \n duration_ms | \n energy | \n instrumentalness | \n key | \n liveness | \n loudness | \n mode | \n speechiness | \n tempo | \n time_signature | \n valence | \n
\n \n \n \n mean | \n 0.170649 | \n 0.589141 | \n 2.422924e+05 | \n 0.711968 | \n 0.213591 | \n 5.328584 | \n 0.216903 | \n -7.127309 | \n 0.581856 | \n 0.146982 | \n 124.640429 | \n 3.957806 | \n 0.418024 | \n
\n \n std | \n 0.246679 | \n 0.173905 | \n 1.220714e+05 | \n 0.204289 | \n 0.335353 | \n 3.673929 | \n 0.173524 | \n 3.646891 | \n 0.493257 | \n 0.136440 | \n 30.809049 | \n 0.356726 | \n 0.236941 | \n
\n \n min | \n 0.000000 | \n 0.000000 | \n 1.578700e+04 | \n 0.000000 | \n 0.000000 | \n 0.000000 | \n 0.000000 | \n -60.000000 | \n 0.000000 | \n 0.000000 | \n 0.000000 | \n 0.000000 | \n 0.000000 | \n
\n \n 25% | \n 0.004320 | \n 0.470000 | \n 1.893220e+05 | \n 0.586000 | \n 0.000000 | \n 2.000000 | \n 0.099900 | \n -8.590000 | \n 0.000000 | \n 0.047500 | \n 97.805000 | \n 4.000000 | \n 0.221000 | \n
\n \n 50% | \n 0.045500 | \n 0.599000 | \n 2.264410e+05 | \n 0.749000 | \n 0.001100 | \n 6.000000 | \n 0.141000 | \n -6.472000 | \n 1.000000 | \n 0.080800 | \n 124.992000 | \n 4.000000 | \n 0.398000 | \n
\n \n 75% | \n 0.237000 | \n 0.724000 | \n 2.787440e+05 | \n 0.878000 | \n 0.394000 | \n 9.000000 | \n 0.300000 | \n -4.827000 | \n 1.000000 | \n 0.223000 | \n 143.188000 | \n 4.000000 | \n 0.597000 | \n
\n \n max | \n 0.996000 | \n 0.981000 | \n 4.995315e+06 | \n 0.999000 | \n 0.995000 | \n 11.000000 | \n 0.995000 | \n 3.108000 | \n 1.000000 | \n 0.966000 | \n 248.028000 | \n 5.000000 | \n 0.983000 | \n
\n \n
\n
"
},
"metadata": {},
"execution_count": 4
}
],
"source": [
"scrobbles.describe()[1:]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" track \\\n",
"34017 Blackbird - Gorgon City Remix \n",
"81549 Lanterns - Dead Man's Chest Remix \n",
"46080 ID Check - Original Mix \n",
"43376 Up & Down \n",
"73918 Cuatro \n",
"... ... \n",
"74391 Julia \n",
"61000 Site Zero / The Vault \n",
"32139 Reminder (feat. How To Dress Well) \n",
"78339 Monsoon \n",
"59279 Let Go (interlude) \n",
"\n",
" album artist \\\n",
"34017 Blackbird EP Joeski \n",
"81549 Lanterns / Lanterns (Dead Man's Chest Remix) Tim Reaper \n",
"46080 Toolroom Ibiza 2019 Ben A \n",
"43376 Emotion EP Purple Disco Machine \n",
"73918 Tomahawk EP Mystic State \n",
"... ... ... \n",
"74391 Void RL Grime \n",
"61000 Void RL Grime \n",
"32139 Void RL Grime \n",
"78339 Void RL Grime \n",
"59279 Void RL Grime \n",
"\n",
" time uri \\\n",
"34017 2020-12-31 18:35:28+00:00 spotify:track:3eGyeq8R8PscX1d13c9eJP \n",
"81549 2020-12-31 18:28:13+00:00 spotify:track:3lc7wN7T29s7uRbPZR0hTH \n",
"46080 2020-12-31 18:22:07+00:00 spotify:track:4x94xmQhUnd59k8oGM7AkG \n",
"43376 2020-12-31 17:52:23+00:00 spotify:track:11DRarpv190YnCAXt85uFA \n",
"73918 2020-12-31 17:00:28+00:00 spotify:track:6JBKvAWsMvo68a9pMa9Ujn \n",
"... ... ... \n",
"74391 2017-11-03 03:35:27+00:00 spotify:track:4or82pWT9zvQNIoGckZiYb \n",
"61000 2017-11-03 03:28:51+00:00 spotify:track:762ME2OHjuGo4xTbfZhpok \n",
"32139 2017-11-03 02:54:37+00:00 spotify:track:2JUdMBlA5JzuemLGzZNDrf \n",
"78339 2017-11-03 02:50:23+00:00 spotify:track:0jYAtTuRsRdHMuvaOXIAj5 \n",
"59279 2017-11-03 02:43:01+00:00 spotify:track:39FvWuHBtYQTJNdisJxZIG \n",
"\n",
" acousticness danceability duration_ms energy instrumentalness key \\\n",
"34017 0.000542 0.803 389834 0.857 0.840 4 \n",
"81549 0.001530 0.537 440255 0.868 0.877 10 \n",
"46080 0.001720 0.809 372614 0.982 0.911 6 \n",
"43376 0.032000 0.758 409961 0.913 0.739 5 \n",
"73918 0.040300 0.621 342866 0.680 0.803 9 \n",
"... ... ... ... ... ... ... \n",
"74391 0.003340 0.573 301429 0.932 0.744 9 \n",
"61000 0.683000 0.289 464015 0.404 0.854 7 \n",
"32139 0.683000 0.593 260075 0.560 0.109 3 \n",
"78339 0.034600 0.546 254815 0.850 0.680 10 \n",
"59279 0.181000 0.361 153346 0.727 0.710 7 \n",
"\n",
" liveness loudness mode speechiness tempo time_signature valence \n",
"34017 0.0787 -7.273 0 0.0449 125.016 4 0.2230 \n",
"81549 0.5730 -7.319 0 0.0618 157.015 4 0.2650 \n",
"46080 0.0657 -8.690 0 0.0460 123.992 4 0.8240 \n",
"43376 0.0304 -6.712 1 0.0518 117.997 4 0.7230 \n",
"73918 0.2890 -10.943 0 0.0484 139.989 4 0.2190 \n",
"... ... ... ... ... ... ... ... \n",
"74391 0.1120 -5.158 0 0.0500 168.008 4 0.1610 \n",
"61000 0.3280 -12.815 0 0.0352 92.873 4 0.0285 \n",
"32139 0.1040 -7.059 0 0.0447 113.895 4 0.3630 \n",
"78339 0.1120 -3.366 0 0.0386 161.996 4 0.3020 \n",
"59279 0.1980 -8.480 1 0.0519 104.380 4 0.0368 \n",
"\n",
"[92217 rows x 18 columns]"
],
"text/html": "\n\n
\n \n \n | \n track | \n album | \n artist | \n time | \n uri | \n acousticness | \n danceability | \n duration_ms | \n energy | \n instrumentalness | \n key | \n liveness | \n loudness | \n mode | \n speechiness | \n tempo | \n time_signature | \n valence | \n
\n \n \n \n 34017 | \n Blackbird - Gorgon City Remix | \n Blackbird EP | \n Joeski | \n 2020-12-31 18:35:28+00:00 | \n spotify:track:3eGyeq8R8PscX1d13c9eJP | \n 0.000542 | \n 0.803 | \n 389834 | \n 0.857 | \n 0.840 | \n 4 | \n 0.0787 | \n -7.273 | \n 0 | \n 0.0449 | \n 125.016 | \n 4 | \n 0.2230 | \n
\n \n 81549 | \n Lanterns - Dead Man's Chest Remix | \n Lanterns / Lanterns (Dead Man's Chest Remix) | \n Tim Reaper | \n 2020-12-31 18:28:13+00:00 | \n spotify:track:3lc7wN7T29s7uRbPZR0hTH | \n 0.001530 | \n 0.537 | \n 440255 | \n 0.868 | \n 0.877 | \n 10 | \n 0.5730 | \n -7.319 | \n 0 | \n 0.0618 | \n 157.015 | \n 4 | \n 0.2650 | \n
\n \n 46080 | \n ID Check - Original Mix | \n Toolroom Ibiza 2019 | \n Ben A | \n 2020-12-31 18:22:07+00:00 | \n spotify:track:4x94xmQhUnd59k8oGM7AkG | \n 0.001720 | \n 0.809 | \n 372614 | \n 0.982 | \n 0.911 | \n 6 | \n 0.0657 | \n -8.690 | \n 0 | \n 0.0460 | \n 123.992 | \n 4 | \n 0.8240 | \n
\n \n 43376 | \n Up & Down | \n Emotion EP | \n Purple Disco Machine | \n 2020-12-31 17:52:23+00:00 | \n spotify:track:11DRarpv190YnCAXt85uFA | \n 0.032000 | \n 0.758 | \n 409961 | \n 0.913 | \n 0.739 | \n 5 | \n 0.0304 | \n -6.712 | \n 1 | \n 0.0518 | \n 117.997 | \n 4 | \n 0.7230 | \n
\n \n 73918 | \n Cuatro | \n Tomahawk EP | \n Mystic State | \n 2020-12-31 17:00:28+00:00 | \n spotify:track:6JBKvAWsMvo68a9pMa9Ujn | \n 0.040300 | \n 0.621 | \n 342866 | \n 0.680 | \n 0.803 | \n 9 | \n 0.2890 | \n -10.943 | \n 0 | \n 0.0484 | \n 139.989 | \n 4 | \n 0.2190 | \n
\n \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n ... | \n
\n \n 74391 | \n Julia | \n Void | \n RL Grime | \n 2017-11-03 03:35:27+00:00 | \n spotify:track:4or82pWT9zvQNIoGckZiYb | \n 0.003340 | \n 0.573 | \n 301429 | \n 0.932 | \n 0.744 | \n 9 | \n 0.1120 | \n -5.158 | \n 0 | \n 0.0500 | \n 168.008 | \n 4 | \n 0.1610 | \n
\n \n 61000 | \n Site Zero / The Vault | \n Void | \n RL Grime | \n 2017-11-03 03:28:51+00:00 | \n spotify:track:762ME2OHjuGo4xTbfZhpok | \n 0.683000 | \n 0.289 | \n 464015 | \n 0.404 | \n 0.854 | \n 7 | \n 0.3280 | \n -12.815 | \n 0 | \n 0.0352 | \n 92.873 | \n 4 | \n 0.0285 | \n
\n \n 32139 | \n Reminder (feat. How To Dress Well) | \n Void | \n RL Grime | \n 2017-11-03 02:54:37+00:00 | \n spotify:track:2JUdMBlA5JzuemLGzZNDrf | \n 0.683000 | \n 0.593 | \n 260075 | \n 0.560 | \n 0.109 | \n 3 | \n 0.1040 | \n -7.059 | \n 0 | \n 0.0447 | \n 113.895 | \n 4 | \n 0.3630 | \n
\n \n 78339 | \n Monsoon | \n Void | \n RL Grime | \n 2017-11-03 02:50:23+00:00 | \n spotify:track:0jYAtTuRsRdHMuvaOXIAj5 | \n 0.034600 | \n 0.546 | \n 254815 | \n 0.850 | \n 0.680 | \n 10 | \n 0.1120 | \n -3.366 | \n 0 | \n 0.0386 | \n 161.996 | \n 4 | \n 0.3020 | \n
\n \n 59279 | \n Let Go (interlude) | \n Void | \n RL Grime | \n 2017-11-03 02:43:01+00:00 | \n spotify:track:39FvWuHBtYQTJNdisJxZIG | \n 0.181000 | \n 0.361 | \n 153346 | \n 0.727 | \n 0.710 | \n 7 | \n 0.1980 | \n -8.480 | \n 1 | \n 0.0519 | \n 104.380 | \n 4 | \n 0.0368 | \n
\n \n
\n
92217 rows × 18 columns
\n
"
},
"metadata": {},
"execution_count": 15
}
],
"source": [
"scrobbles.sort_values(by=\"time\", ascending=False)"
]
},
{
"source": [
"# Rap\n",
"\n",
"## Descriptor Stats"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"28 days spent listening since Nov. 2017\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
" acousticness danceability duration_ms energy instrumentalness \\\n",
"mean 0.179491 0.654732 217263.266814 0.711093 0.007111 \n",
"std 0.182540 0.149069 60534.505476 0.142709 0.047975 \n",
"min 0.000063 0.261000 81967.000000 0.274000 0.000000 \n",
"25% 0.039900 0.546000 180893.000000 0.604000 0.000000 \n",
"50% 0.130000 0.668000 208013.000000 0.720000 0.000000 \n",
"75% 0.253000 0.759000 245200.000000 0.821000 0.000035 \n",
"max 0.864000 0.975000 774920.000000 0.993000 0.847000 \n",
"\n",
" key liveness loudness mode speechiness tempo \\\n",
"mean 5.231103 0.243851 -6.739009 0.653965 0.275643 120.313818 \n",
"std 3.752245 0.167658 2.351719 0.475725 0.128600 31.569949 \n",
"min 0.000000 0.033300 -17.485000 0.000000 0.037800 61.113000 \n",
"25% 1.000000 0.114000 -8.324000 0.000000 0.186000 91.973000 \n",
"50% 6.000000 0.184000 -6.553000 1.000000 0.282000 120.051000 \n",
"75% 8.000000 0.339000 -5.146000 1.000000 0.362000 140.144000 \n",
"max 11.000000 0.979000 -1.354000 1.000000 0.827000 207.982000 \n",
"\n",
" time_signature valence \n",
"mean 4.008950 0.465955 \n",
"std 0.252267 0.222555 \n",
"min 1.000000 0.027200 \n",
"25% 4.000000 0.293000 \n",
"50% 4.000000 0.457000 \n",
"75% 4.000000 0.628000 \n",
"max 5.000000 0.961000 "
],
"text/html": "\n\n
\n \n \n | \n acousticness | \n danceability | \n duration_ms | \n energy | \n instrumentalness | \n key | \n liveness | \n loudness | \n mode | \n speechiness | \n tempo | \n time_signature | \n valence | \n
\n \n \n \n mean | \n 0.179491 | \n 0.654732 | \n 217263.266814 | \n 0.711093 | \n 0.007111 | \n 5.231103 | \n 0.243851 | \n -6.739009 | \n 0.653965 | \n 0.275643 | \n 120.313818 | \n 4.008950 | \n 0.465955 | \n
\n \n std | \n 0.182540 | \n 0.149069 | \n 60534.505476 | \n 0.142709 | \n 0.047975 | \n 3.752245 | \n 0.167658 | \n 2.351719 | \n 0.475725 | \n 0.128600 | \n 31.569949 | \n 0.252267 | \n 0.222555 | \n
\n \n min | \n 0.000063 | \n 0.261000 | \n 81967.000000 | \n 0.274000 | \n 0.000000 | \n 0.000000 | \n 0.033300 | \n -17.485000 | \n 0.000000 | \n 0.037800 | \n 61.113000 | \n 1.000000 | \n 0.027200 | \n
\n \n 25% | \n 0.039900 | \n 0.546000 | \n 180893.000000 | \n 0.604000 | \n 0.000000 | \n 1.000000 | \n 0.114000 | \n -8.324000 | \n 0.000000 | \n 0.186000 | \n 91.973000 | \n 4.000000 | \n 0.293000 | \n
\n \n 50% | \n 0.130000 | \n 0.668000 | \n 208013.000000 | \n 0.720000 | \n 0.000000 | \n 6.000000 | \n 0.184000 | \n -6.553000 | \n 1.000000 | \n 0.282000 | \n 120.051000 | \n 4.000000 | \n 0.457000 | \n
\n \n 75% | \n 0.253000 | \n 0.759000 | \n 245200.000000 | \n 0.821000 | \n 0.000035 | \n 8.000000 | \n 0.339000 | \n -5.146000 | \n 1.000000 | \n 0.362000 | \n 140.144000 | \n 4.000000 | \n 0.628000 | \n
\n \n max | \n 0.864000 | \n 0.975000 | \n 774920.000000 | \n 0.993000 | \n 0.847000 | \n 11.000000 | \n 0.979000 | \n -1.354000 | \n 1.000000 | \n 0.827000 | \n 207.982000 | \n 5.000000 | \n 0.961000 | \n
\n \n
\n
"
},
"metadata": {},
"execution_count": 10
}
],
"source": [
"rap = get_playlist(\"RAP\", spotnet)\n",
"rap_frame = pd.merge(track_frame(rap.tracks), scrobbles, on=['track', 'artist']) # FILTER SCROBBLES\n",
"rap_frame = rap_frame.sort_values(by=\"time\", ascending=False) # SORT\n",
"rap_frame = rap_frame.loc[:, descriptor_headers] # DESCRIPTORS\n",
"\n",
"total_time = rap_frame[\"duration_ms\"].sum() / (1000 * 60 * 60 * 24)\n",
"print(f'{total_time:.0f} days spent listening since Nov. 2017')\n",
"\n",
"rap_frame.describe()[1:]"
]
},
{
"source": [
"# Playlist Comparisons"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"playlist_names = [\"RAP\", \"EDM\", \"ROCK\", \"METAL\", \"JAZZ\", \"POP\"]\n",
"playlists = [get_playlist(i, spotnet) for i in playlist_names]\n",
"\n",
"filtered_playlists = [pd.merge(track_frame(i.tracks), scrobbles, on=['track', 'artist']) for i in playlists]\n",
"filtered_playlists = [i.drop_duplicates(['uri']) for i in filtered_playlists]\n",
"filtered_playlists = [i.loc[:, float_headers] for i in filtered_playlists]\n",
"\n",
"playlist_mean = [i.mean() for i in filtered_playlists]\n",
"playlist_std = [i.std() for i in filtered_playlists]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "