{ "cells": [ { "cell_type": "markdown", "id": "persistent-friendship", "metadata": {}, "source": [ "Spotify is one of the digital services that helped me to tide over the somber of Covid-19 lockdown. Therefore, I chose to explore its data in my first attempt to write code in python. After a bunch of trials and errors, I managed to extract songs and its features from spotify through the below codes in Jupyter lab notebook. Web reference to these codes are at the end." ] }, { "cell_type": "code", "execution_count": null, "id": "cooperative-little", "metadata": {}, "outputs": [], "source": [ "Part 1- The first step to extract data from spotify is to set up client cerdintials using spotify's API key" ] }, { "cell_type": "code", "execution_count": 29, "id": "eleven-smart", "metadata": {}, "outputs": [], "source": [ "# I found the API key from the spotify's developers website\n", "# https://developer.spotify.com/dashboard/login\n", "# PS- The client id expires after an hour of extraction\n", "\n", "import requests\n", "\n", "CLIENT_ID = 'add the client id from the website'\n", "CLIENT_SECRET = 'add the client secret from the website'\n", "\n", "AUTH_URL = 'https://accounts.spotify.com/api/token'\n", "\n", "auth_response = requests.post(AUTH_URL, {\n", " 'grant_type': 'client_credentials',\n", " 'client_id': CLIENT_ID,\n", " 'client_secret': CLIENT_SECRET,})\n", "\n", "# convert the response to JSON\n", "auth_response_data = auth_response.json()\n", "\n", "# save the access token\n", "access_token = auth_response_data['access_token']\n", "\n", "headers = {\n", " 'Authorization': 'Bearer {token}'.format(token=access_token)\n", "}\n", "\n", "# base URL of all Spotify API endpoints\n", "BASE_URL = 'https://api.spotify.com/v1/'" ] }, { "cell_type": "code", "execution_count": null, "id": "continuous-oliver", "metadata": {}, "outputs": [], "source": [ "Part 2- Below code allowed me to extract features of the song that topped the global charts " ] }, { "cell_type": "code", "execution_count": 33, "id": "stable-strain", "metadata": {}, "outputs": [], "source": [ "# Dakiti song was streamed the most globally in the first week of Jan 2021 \n", "# Refer- https://spotifycharts.com/regional/global/weekly/2021-01-01--2021-01-08\n", "track_id = '4MzXwWMhyBbmu6hOcLVD49?si=7d86fb3ca8fe410a' \n", "\n", "# actual GET request with proper header\n", "r = requests.get(BASE_URL + 'audio-features/' + track_id, headers=headers)\n", "\n", "# description of the result- https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-audio-features" ] }, { "cell_type": "markdown", "id": "funny-runner", "metadata": {}, "source": [ "Part 3- Below code allowed me to extract the features of the songs from the albums of an artist. I chose one of the artists of Dakiti." ] }, { "cell_type": "code", "execution_count": 40, "id": "brutal-governor", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "EL ÚLTIMO TOUR DEL MUNDO --- 2020-11-27\n", "LAS QUE NO IBAN A SALIR --- 2020-05-10\n", "YHLQMDLG --- 2020-02-28\n", "OASIS --- 2019-06-28\n", "X 100PRE --- 2018-12-23\n" ] } ], "source": [ "for album in d['items']:\n", " print(album['name'], ' --- ', album['release_date'])" ] }, { "cell_type": "code", "execution_count": 50, "id": "under-philadelphia", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "EL ÚLTIMO TOUR DEL MUNDO\n", "LAS QUE NO IBAN A SALIR\n", "YHLQMDLG\n", "OASIS\n", "X 100PRE\n" ] } ], "source": [ "data = [] # will hold all track info\n", "albums = [] # to keep track of duplicates\n", "\n", "# loop over albums and get all tracks\n", "for album in d['items']:\n", " album_name = album['name']\n", " \n", "# here's a hacky way to skip over albums we've already grabbed\n", " trim_name = album_name.split('(')[0].strip()\n", "\n", " # this takes a few seconds so let's keep track of progress \n", " print(album_name)\n", " \n", " # pull all tracks from this album\n", " r = requests.get(BASE_URL + 'albums/' + album['id'] + '/tracks', \n", " headers=headers)\n", " tracks = r.json()['items']\n", " \n", " for track in tracks:\n", " # get audio features (key, liveness, danceability, ...)\n", " f = requests.get(BASE_URL + 'audio-features/' + track['id'], \n", " headers=headers)\n", " f = f.json()\n", " \n", " # combine with album info\n", " f.update({\n", " 'track_name': track['name'],\n", " 'album_name': album_name,\n", " 'short_album_name': trim_name,\n", " 'release_date': album['release_date'],\n", " 'album_id': album['id']\n", " })\n", " \n", " data.append(f)" ] }, { "cell_type": "code", "execution_count": 51, "id": "favorite-theology", "metadata": {}, "outputs": [], "source": [ "#create data frame of songs in the artist's spotify album\n", "\n", "import pandas as pd\n", "df = pd.DataFrame(data)" ] }, { "cell_type": "code", "execution_count": 71, "id": "immune-search", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
danceabilityenergykeyloudnessmodespeechinessacousticnessinstrumentalnesslivenessvalence...uritrack_hrefanalysis_urlduration_mstime_signaturetrack_namealbum_nameshort_album_namerelease_datealbum_id
00.7160.5225-6.83410.05820.16600.0000650.11300.224...spotify:track:36DHxTW2xdr9GG15T9oK9Lhttps://api.spotify.com/v1/tracks/36DHxTW2xdr9...https://api.spotify.com/v1/audio-analysis/36DH...1651994EL MUNDO ES MÍOEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
10.8110.63710-4.83500.05910.23400.0005720.11800.471...spotify:track:5RubKOuDoPn5Kj5TLVxSxYhttps://api.spotify.com/v1/tracks/5RubKOuDoPn5...https://api.spotify.com/v1/audio-analysis/5Rub...1300144TE MUDASTEEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
20.8600.72511-6.70010.24900.04640.0000910.09940.375...spotify:track:0tjZv2hChdHZCW1zFXpy1Jhttps://api.spotify.com/v1/tracks/0tjZv2hChdHZ...https://api.spotify.com/v1/audio-analysis/0tjZ...1621514HOY COBRÉEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
30.7620.8614-4.07500.06520.13900.0000010.09560.588...spotify:track:0Lsis3LB0XAK6XlTHXaJk2https://api.spotify.com/v1/tracks/0Lsis3LB0XAK...https://api.spotify.com/v1/audio-analysis/0Lsi...2136094MALDITA POBREZAEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
40.8560.6187-4.89210.28600.03030.0000000.08660.391...spotify:track:2XIc1pqjXV3Cr2BQUGNBckhttps://api.spotify.com/v1/tracks/2XIc1pqjXV3C...https://api.spotify.com/v1/audio-analysis/2XIc...2032014LA NOCHE DE ANOCHEEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
\n", "

5 rows × 23 columns

\n", "
" ], "text/plain": [ " danceability energy key loudness mode speechiness acousticness \\\n", "0 0.716 0.522 5 -6.834 1 0.0582 0.1660 \n", "1 0.811 0.637 10 -4.835 0 0.0591 0.2340 \n", "2 0.860 0.725 11 -6.700 1 0.2490 0.0464 \n", "3 0.762 0.861 4 -4.075 0 0.0652 0.1390 \n", "4 0.856 0.618 7 -4.892 1 0.2860 0.0303 \n", "\n", " instrumentalness liveness valence ... \\\n", "0 0.000065 0.1130 0.224 ... \n", "1 0.000572 0.1180 0.471 ... \n", "2 0.000091 0.0994 0.375 ... \n", "3 0.000001 0.0956 0.588 ... \n", "4 0.000000 0.0866 0.391 ... \n", "\n", " uri \\\n", "0 spotify:track:36DHxTW2xdr9GG15T9oK9L \n", "1 spotify:track:5RubKOuDoPn5Kj5TLVxSxY \n", "2 spotify:track:0tjZv2hChdHZCW1zFXpy1J \n", "3 spotify:track:0Lsis3LB0XAK6XlTHXaJk2 \n", "4 spotify:track:2XIc1pqjXV3Cr2BQUGNBck \n", "\n", " track_href \\\n", "0 https://api.spotify.com/v1/tracks/36DHxTW2xdr9... \n", "1 https://api.spotify.com/v1/tracks/5RubKOuDoPn5... \n", "2 https://api.spotify.com/v1/tracks/0tjZv2hChdHZ... \n", "3 https://api.spotify.com/v1/tracks/0Lsis3LB0XAK... \n", "4 https://api.spotify.com/v1/tracks/2XIc1pqjXV3C... \n", "\n", " analysis_url duration_ms \\\n", "0 https://api.spotify.com/v1/audio-analysis/36DH... 165199 \n", "1 https://api.spotify.com/v1/audio-analysis/5Rub... 130014 \n", "2 https://api.spotify.com/v1/audio-analysis/0tjZ... 162151 \n", "3 https://api.spotify.com/v1/audio-analysis/0Lsi... 213609 \n", "4 https://api.spotify.com/v1/audio-analysis/2XIc... 203201 \n", "\n", " time_signature track_name album_name \\\n", "0 4 EL MUNDO ES MÍO EL ÚLTIMO TOUR DEL MUNDO \n", "1 4 TE MUDASTE EL ÚLTIMO TOUR DEL MUNDO \n", "2 4 HOY COBRÉ EL ÚLTIMO TOUR DEL MUNDO \n", "3 4 MALDITA POBREZA EL ÚLTIMO TOUR DEL MUNDO \n", "4 4 LA NOCHE DE ANOCHE EL ÚLTIMO TOUR DEL MUNDO \n", "\n", " short_album_name release_date album_id \n", "0 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "1 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "2 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "3 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "4 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "\n", "[5 rows x 23 columns]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(5)" ] }, { "cell_type": "code", "execution_count": 72, "id": "driving-province", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
danceabilityenergykeyloudnessmodespeechinessacousticnessinstrumentalnesslivenessvalence...uritrack_hrefanalysis_urlduration_mstime_signaturetrack_namealbum_nameshort_album_namerelease_datealbum_id
00.7160.5225-6.83410.05820.16600.0000650.11300.224...spotify:track:36DHxTW2xdr9GG15T9oK9Lhttps://api.spotify.com/v1/tracks/36DHxTW2xdr9...https://api.spotify.com/v1/audio-analysis/36DH...1651994EL MUNDO ES MÍOEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
10.8110.63710-4.83500.05910.23400.0005720.11800.471...spotify:track:5RubKOuDoPn5Kj5TLVxSxYhttps://api.spotify.com/v1/tracks/5RubKOuDoPn5...https://api.spotify.com/v1/audio-analysis/5Rub...1300144TE MUDASTEEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
20.8600.72511-6.70010.24900.04640.0000910.09940.375...spotify:track:0tjZv2hChdHZCW1zFXpy1Jhttps://api.spotify.com/v1/tracks/0tjZv2hChdHZ...https://api.spotify.com/v1/audio-analysis/0tjZ...1621514HOY COBRÉEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
30.7620.8614-4.07500.06520.13900.0000010.09560.588...spotify:track:0Lsis3LB0XAK6XlTHXaJk2https://api.spotify.com/v1/tracks/0Lsis3LB0XAK...https://api.spotify.com/v1/audio-analysis/0Lsi...2136094MALDITA POBREZAEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
40.8560.6187-4.89210.28600.03030.0000000.08660.391...spotify:track:2XIc1pqjXV3Cr2BQUGNBckhttps://api.spotify.com/v1/tracks/2XIc1pqjXV3C...https://api.spotify.com/v1/audio-analysis/2XIc...2032014LA NOCHE DE ANOCHEEL ÚLTIMO TOUR DEL MUNDOEL ÚLTIMO TOUR DEL MUNDO2020-11-272d9BCZeAAhiZWPpbX9aPCW
..................................................................
630.7870.7050-7.58210.06950.13700.0000010.10800.499...spotify:track:5mj8WVFcKdGA8p9HWGTSLchttps://api.spotify.com/v1/tracks/5mj8WVFcKdGA...https://api.spotify.com/v1/audio-analysis/5mj8...1886544Cuando PerriabasX 100PREX 100PRE2018-12-237CjJb2mikwAWA1V6kewFBF
640.6550.7250-5.49710.18800.03270.0026400.06110.326...spotify:track:1khmgu0pveJbkbpbkyvcQvhttps://api.spotify.com/v1/tracks/1khmgu0pveJb...https://api.spotify.com/v1/audio-analysis/1khm...3005794La RomanaX 100PREX 100PRE2018-12-237CjJb2mikwAWA1V6kewFBF
650.7670.3790-10.34810.03850.66800.0001450.21700.252...spotify:track:69ZaPBHhRMRDjRpW1ivnOUhttps://api.spotify.com/v1/tracks/69ZaPBHhRMRD...https://api.spotify.com/v1/audio-analysis/69Za...2305784Como AntesX 100PREX 100PRE2018-12-237CjJb2mikwAWA1V6kewFBF
660.6000.5280-6.55410.03080.26300.0000000.58800.142...spotify:track:6pZHZndlo57dPCYnvlYFOEhttps://api.spotify.com/v1/tracks/6pZHZndlo57d...https://api.spotify.com/v1/audio-analysis/6pZH...2848534RLNDTX 100PREX 100PRE2018-12-237CjJb2mikwAWA1V6kewFBF
670.7590.5369-6.66300.17300.82100.0000050.10700.439...spotify:track:2OWVCFTolecLiGZPquvWvThttps://api.spotify.com/v1/tracks/2OWVCFTolecL...https://api.spotify.com/v1/audio-analysis/2OWV...2080804Estamos BienX 100PREX 100PRE2018-12-237CjJb2mikwAWA1V6kewFBF
\n", "

68 rows × 23 columns

\n", "
" ], "text/plain": [ " danceability energy key loudness mode speechiness acousticness \\\n", "0 0.716 0.522 5 -6.834 1 0.0582 0.1660 \n", "1 0.811 0.637 10 -4.835 0 0.0591 0.2340 \n", "2 0.860 0.725 11 -6.700 1 0.2490 0.0464 \n", "3 0.762 0.861 4 -4.075 0 0.0652 0.1390 \n", "4 0.856 0.618 7 -4.892 1 0.2860 0.0303 \n", ".. ... ... ... ... ... ... ... \n", "63 0.787 0.705 0 -7.582 1 0.0695 0.1370 \n", "64 0.655 0.725 0 -5.497 1 0.1880 0.0327 \n", "65 0.767 0.379 0 -10.348 1 0.0385 0.6680 \n", "66 0.600 0.528 0 -6.554 1 0.0308 0.2630 \n", "67 0.759 0.536 9 -6.663 0 0.1730 0.8210 \n", "\n", " instrumentalness liveness valence ... \\\n", "0 0.000065 0.1130 0.224 ... \n", "1 0.000572 0.1180 0.471 ... \n", "2 0.000091 0.0994 0.375 ... \n", "3 0.000001 0.0956 0.588 ... \n", "4 0.000000 0.0866 0.391 ... \n", ".. ... ... ... ... \n", "63 0.000001 0.1080 0.499 ... \n", "64 0.002640 0.0611 0.326 ... \n", "65 0.000145 0.2170 0.252 ... \n", "66 0.000000 0.5880 0.142 ... \n", "67 0.000005 0.1070 0.439 ... \n", "\n", " uri \\\n", "0 spotify:track:36DHxTW2xdr9GG15T9oK9L \n", "1 spotify:track:5RubKOuDoPn5Kj5TLVxSxY \n", "2 spotify:track:0tjZv2hChdHZCW1zFXpy1J \n", "3 spotify:track:0Lsis3LB0XAK6XlTHXaJk2 \n", "4 spotify:track:2XIc1pqjXV3Cr2BQUGNBck \n", ".. ... \n", "63 spotify:track:5mj8WVFcKdGA8p9HWGTSLc \n", "64 spotify:track:1khmgu0pveJbkbpbkyvcQv \n", "65 spotify:track:69ZaPBHhRMRDjRpW1ivnOU \n", "66 spotify:track:6pZHZndlo57dPCYnvlYFOE \n", "67 spotify:track:2OWVCFTolecLiGZPquvWvT \n", "\n", " track_href \\\n", "0 https://api.spotify.com/v1/tracks/36DHxTW2xdr9... \n", "1 https://api.spotify.com/v1/tracks/5RubKOuDoPn5... \n", "2 https://api.spotify.com/v1/tracks/0tjZv2hChdHZ... \n", "3 https://api.spotify.com/v1/tracks/0Lsis3LB0XAK... \n", "4 https://api.spotify.com/v1/tracks/2XIc1pqjXV3C... \n", ".. ... \n", "63 https://api.spotify.com/v1/tracks/5mj8WVFcKdGA... \n", "64 https://api.spotify.com/v1/tracks/1khmgu0pveJb... \n", "65 https://api.spotify.com/v1/tracks/69ZaPBHhRMRD... \n", "66 https://api.spotify.com/v1/tracks/6pZHZndlo57d... \n", "67 https://api.spotify.com/v1/tracks/2OWVCFTolecL... \n", "\n", " analysis_url duration_ms \\\n", "0 https://api.spotify.com/v1/audio-analysis/36DH... 165199 \n", "1 https://api.spotify.com/v1/audio-analysis/5Rub... 130014 \n", "2 https://api.spotify.com/v1/audio-analysis/0tjZ... 162151 \n", "3 https://api.spotify.com/v1/audio-analysis/0Lsi... 213609 \n", "4 https://api.spotify.com/v1/audio-analysis/2XIc... 203201 \n", ".. ... ... \n", "63 https://api.spotify.com/v1/audio-analysis/5mj8... 188654 \n", "64 https://api.spotify.com/v1/audio-analysis/1khm... 300579 \n", "65 https://api.spotify.com/v1/audio-analysis/69Za... 230578 \n", "66 https://api.spotify.com/v1/audio-analysis/6pZH... 284853 \n", "67 https://api.spotify.com/v1/audio-analysis/2OWV... 208080 \n", "\n", " time_signature track_name album_name \\\n", "0 4 EL MUNDO ES MÍO EL ÚLTIMO TOUR DEL MUNDO \n", "1 4 TE MUDASTE EL ÚLTIMO TOUR DEL MUNDO \n", "2 4 HOY COBRÉ EL ÚLTIMO TOUR DEL MUNDO \n", "3 4 MALDITA POBREZA EL ÚLTIMO TOUR DEL MUNDO \n", "4 4 LA NOCHE DE ANOCHE EL ÚLTIMO TOUR DEL MUNDO \n", ".. ... ... ... \n", "63 4 Cuando Perriabas X 100PRE \n", "64 4 La Romana X 100PRE \n", "65 4 Como Antes X 100PRE \n", "66 4 RLNDT X 100PRE \n", "67 4 Estamos Bien X 100PRE \n", "\n", " short_album_name release_date album_id \n", "0 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "1 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "2 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "3 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", "4 EL ÚLTIMO TOUR DEL MUNDO 2020-11-27 2d9BCZeAAhiZWPpbX9aPCW \n", ".. ... ... ... \n", "63 X 100PRE 2018-12-23 7CjJb2mikwAWA1V6kewFBF \n", "64 X 100PRE 2018-12-23 7CjJb2mikwAWA1V6kewFBF \n", "65 X 100PRE 2018-12-23 7CjJb2mikwAWA1V6kewFBF \n", "66 X 100PRE 2018-12-23 7CjJb2mikwAWA1V6kewFBF \n", "67 X 100PRE 2018-12-23 7CjJb2mikwAWA1V6kewFBF \n", "\n", "[68 rows x 23 columns]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Display the first rows and the last rows of the dataframe\n", "\n", "df.head(-1)" ] }, { "cell_type": "code", "execution_count": 65, "id": "decent-anaheim", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 69 entries, 0 to 68\n", "Data columns (total 23 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 danceability 69 non-null float64\n", " 1 energy 69 non-null float64\n", " 2 key 69 non-null int64 \n", " 3 loudness 69 non-null float64\n", " 4 mode 69 non-null int64 \n", " 5 speechiness 69 non-null float64\n", " 6 acousticness 69 non-null float64\n", " 7 instrumentalness 69 non-null float64\n", " 8 liveness 69 non-null float64\n", " 9 valence 69 non-null float64\n", " 10 tempo 69 non-null float64\n", " 11 type 69 non-null object \n", " 12 id 69 non-null object \n", " 13 uri 69 non-null object \n", " 14 track_href 69 non-null object \n", " 15 analysis_url 69 non-null object \n", " 16 duration_ms 69 non-null int64 \n", " 17 time_signature 69 non-null int64 \n", " 18 track_name 69 non-null object \n", " 19 album_name 69 non-null object \n", " 20 short_album_name 69 non-null object \n", " 21 release_date 69 non-null object \n", " 22 album_id 69 non-null object \n", "dtypes: float64(9), int64(4), object(10)\n", "memory usage: 12.5+ KB\n", "None\n" ] } ], "source": [ "# Data types of columns\n", "print(df.info())" ] }, { "cell_type": "code", "execution_count": 85, "id": "connected-fiction", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
danceabilityenergykeyloudnessmodespeechinessacousticnessinstrumentalnesslivenessvalence...uritrack_hrefanalysis_urlduration_mstime_signaturetrack_namealbum_nameshort_album_namerelease_datealbum_id
count69.00000069.00000069.00000069.00000069.00000069.00000069.00000069.00000069.00000069.000000...69696969.00000069.06969696969
uniqueNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...696969NaNNaN695555
topNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...spotify:track:5RubKOuDoPn5Kj5TLVxSxYhttps://api.spotify.com/v1/tracks/4UEuIEv9Wc3w...https://api.spotify.com/v1/audio-analysis/53v2...NaNNaNSer BichoteYHLQMDLGYHLQMDLG2020-02-285lJqux7orBlA1QzyiBGti1
freqNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN...111NaNNaN120202020
mean0.7441300.6639284.840580-6.0682610.5507250.1164680.2095740.0002810.1562830.514128...NaNNaNNaN198843.3913044.0NaNNaNNaNNaNNaN
std0.1068350.1229303.6324651.7139210.5010650.0931540.2109000.0012580.1129640.240325...NaNNaNNaN39238.1776950.0NaNNaNNaNNaNNaN
min0.4300000.3790000.000000-10.8050000.0000000.0281000.0103000.0000000.0611000.050800...NaNNaNNaN130014.0000004.0NaNNaNNaNNaNNaN
25%0.6830000.5800001.000000-7.1250000.0000000.0582000.0589000.0000000.0980000.326000...NaNNaNNaN165199.0000004.0NaNNaNNaNNaNNaN
50%0.7620000.6560005.000000-5.7490001.0000000.0772000.1390000.0000050.1080000.514000...NaNNaNNaN196500.0000004.0NaNNaNNaNNaNNaN
75%0.8260000.7640007.000000-4.8350001.0000000.1310000.2870000.0000650.1530000.685000...NaNNaNNaN224512.0000004.0NaNNaNNaNNaNNaN
max0.9000000.88100011.000000-2.9790001.0000000.4020000.8690000.0099100.6590000.962000...NaNNaNNaN300579.0000004.0NaNNaNNaNNaNNaN
\n", "

11 rows × 23 columns

\n", "
" ], "text/plain": [ " danceability energy key loudness mode speechiness \\\n", "count 69.000000 69.000000 69.000000 69.000000 69.000000 69.000000 \n", "unique NaN NaN NaN NaN NaN NaN \n", "top NaN NaN NaN NaN NaN NaN \n", "freq NaN NaN NaN NaN NaN NaN \n", "mean 0.744130 0.663928 4.840580 -6.068261 0.550725 0.116468 \n", "std 0.106835 0.122930 3.632465 1.713921 0.501065 0.093154 \n", "min 0.430000 0.379000 0.000000 -10.805000 0.000000 0.028100 \n", "25% 0.683000 0.580000 1.000000 -7.125000 0.000000 0.058200 \n", "50% 0.762000 0.656000 5.000000 -5.749000 1.000000 0.077200 \n", "75% 0.826000 0.764000 7.000000 -4.835000 1.000000 0.131000 \n", "max 0.900000 0.881000 11.000000 -2.979000 1.000000 0.402000 \n", "\n", " acousticness instrumentalness liveness valence ... \\\n", "count 69.000000 69.000000 69.000000 69.000000 ... \n", "unique NaN NaN NaN NaN ... \n", "top NaN NaN NaN NaN ... \n", "freq NaN NaN NaN NaN ... \n", "mean 0.209574 0.000281 0.156283 0.514128 ... \n", "std 0.210900 0.001258 0.112964 0.240325 ... \n", "min 0.010300 0.000000 0.061100 0.050800 ... \n", "25% 0.058900 0.000000 0.098000 0.326000 ... \n", "50% 0.139000 0.000005 0.108000 0.514000 ... \n", "75% 0.287000 0.000065 0.153000 0.685000 ... \n", "max 0.869000 0.009910 0.659000 0.962000 ... \n", "\n", " uri \\\n", "count 69 \n", "unique 69 \n", "top spotify:track:5RubKOuDoPn5Kj5TLVxSxY \n", "freq 1 \n", "mean NaN \n", "std NaN \n", "min NaN \n", "25% NaN \n", "50% NaN \n", "75% NaN \n", "max NaN \n", "\n", " track_href \\\n", "count 69 \n", "unique 69 \n", "top https://api.spotify.com/v1/tracks/4UEuIEv9Wc3w... \n", "freq 1 \n", "mean NaN \n", "std NaN \n", "min NaN \n", "25% NaN \n", "50% NaN \n", "75% NaN \n", "max NaN \n", "\n", " analysis_url duration_ms \\\n", "count 69 69.000000 \n", "unique 69 NaN \n", "top https://api.spotify.com/v1/audio-analysis/53v2... NaN \n", "freq 1 NaN \n", "mean NaN 198843.391304 \n", "std NaN 39238.177695 \n", "min NaN 130014.000000 \n", "25% NaN 165199.000000 \n", "50% NaN 196500.000000 \n", "75% NaN 224512.000000 \n", "max NaN 300579.000000 \n", "\n", " time_signature track_name album_name short_album_name release_date \\\n", "count 69.0 69 69 69 69 \n", "unique NaN 69 5 5 5 \n", "top NaN Ser Bichote YHLQMDLG YHLQMDLG 2020-02-28 \n", "freq NaN 1 20 20 20 \n", "mean 4.0 NaN NaN NaN NaN \n", "std 0.0 NaN NaN NaN NaN \n", "min 4.0 NaN NaN NaN NaN \n", "25% 4.0 NaN NaN NaN NaN \n", "50% 4.0 NaN NaN NaN NaN \n", "75% 4.0 NaN NaN NaN NaN \n", "max 4.0 NaN NaN NaN NaN \n", "\n", " album_id \n", "count 69 \n", "unique 5 \n", "top 5lJqux7orBlA1QzyiBGti1 \n", "freq 20 \n", "mean NaN \n", "std NaN \n", "min NaN \n", "25% NaN \n", "50% NaN \n", "75% NaN \n", "max NaN \n", "\n", "[11 rows x 23 columns]" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# summary of the data frame\n", "df.describe(include='all') " ] }, { "cell_type": "code", "execution_count": null, "id": "collaborative-camera", "metadata": {}, "outputs": [], "source": [ "# save the dataframe into csv\n", "df.to_csv(\"spotify_music.csv\")" ] }, { "cell_type": "code", "execution_count": 92, "id": "legitimate-pilot", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9\n" ] } ], "source": [ "print(df.max()['danceability'])\n" ] }, { "cell_type": "code", "execution_count": 95, "id": "postal-eleven", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "danceability 0.9\n", "energy 0.603\n", "key 2\n", "loudness -5.313\n", "mode 1\n", "speechiness 0.0646\n", "acousticness 0.402\n", "instrumentalness 0.000005\n", "liveness 0.134\n", "valence 0.824\n", "tempo 129.928\n", "type audio_features\n", "id 41wtwzCZkXwpnakmwJ239F\n", "uri spotify:track:41wtwzCZkXwpnakmwJ239F\n", "track_href https://api.spotify.com/v1/tracks/41wtwzCZkXwp...\n", "analysis_url https://api.spotify.com/v1/audio-analysis/41wt...\n", "duration_ms 170972\n", "time_signature 4\n", "track_name Si Veo a Tu Mamá\n", "album_name YHLQMDLG\n", "short_album_name YHLQMDLG\n", "release_date 2020-02-28\n", "album_id 5lJqux7orBlA1QzyiBGti1\n", "Name: 26, dtype: object" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# find the row of max value of danceability\n", "\n", "df.loc[df['danceability'].idxmax()]\n" ] }, { "cell_type": "code", "execution_count": 110, "id": "ready-jewel", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "danceability 0.9\n", "energy 0.603\n", "key 2\n", "loudness -5.313\n", "mode 1\n", "speechiness 0.0646\n", "acousticness 0.402\n", "instrumentalness 0.000005\n", "liveness 0.134\n", "valence 0.824\n", "tempo 129.928\n", "type audio_features\n", "id 41wtwzCZkXwpnakmwJ239F\n", "uri spotify:track:41wtwzCZkXwpnakmwJ239F\n", "track_href https://api.spotify.com/v1/tracks/41wtwzCZkXwp...\n", "analysis_url https://api.spotify.com/v1/audio-analysis/41wt...\n", "duration_ms 170972\n", "time_signature 4\n", "track_name Si Veo a Tu Mamá\n", "album_name YHLQMDLG\n", "short_album_name YHLQMDLG\n", "release_date 2020-02-28\n", "album_id 5lJqux7orBlA1QzyiBGti1\n", "Name: 26, dtype: object\n" ] } ], "source": [ "# create a dataframe of row with maximum danceability \n", "\n", "df2= df.loc[df['danceability'].idxmax()]\n", "print (df2)\n" ] }, { "cell_type": "code", "execution_count": 122, "id": "illegal-grade", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "danceability False\n", "energy False\n", "key False\n", "loudness False\n", "mode False\n", "speechiness False\n", "acousticness False\n", "instrumentalness False\n", "liveness False\n", "valence False\n", "tempo False\n", "type False\n", "id False\n", "uri False\n", "track_href False\n", "analysis_url False\n", "duration_ms False\n", "time_signature False\n", "track_name False\n", "album_name False\n", "short_album_name False\n", "release_date False\n", "album_id False\n", "Name: 26, dtype: bool" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2 == r" ] }, { "cell_type": "code", "execution_count": null, "id": "reasonable-smile", "metadata": {}, "outputs": [], "source": [ "Refrence of the codes-\n", "Steven Morse's blog-- https://stmorse.github.io/journal/spotify-api.html\n", "Ujaval Gandhi's blog-- https://spatialthoughts.com/courses/python-foundation-for-spatial-analysis/" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }