My Top Played Artists on Spotify

December 10, 2017

ANTH 4020: Digital Anthropology

Final Mixed-media Project

In [1]:
import pandas as pd
import numpy as np
import json
from pandas.io.json import json_normalize
In [2]:
data = pd.read_json('/Users/troy/Github/courses/DANTH/myTopArtists.json')
name = []
pop = []
genres = []
for item in data['items']:
    name.append(item['name'])
    pop.append(item['popularity'])
    genres.append(item['genres'])

df_columns = {'Artists': name,'Popularities': pop,'Genres': genres}
df = pd.DataFrame(data=df_columns)
df
Out[2]:
Artists Genres Popularities
0 Metallica [alternative metal, alternative rock, hard roc... 86
1 Red Hot Chili Peppers [alternative rock, funk metal, funk rock, mode... 88
2 Jazzinuf [chillhop] 57
3 The Bloody Beetroots [alternative dance, complextro, edm, electro h... 58
4 Neil Young [album rock, classic rock, mellow gold, perman... 74
5 Grateful Dead [blues-rock, classic rock, folk rock, jam band... 72
6 Mark Knopfler [blues-rock, mellow gold, roots rock, singer-s... 65
7 Umphrey's McGee [jam band, modern blues, progressive bluegrass] 55
8 Random Rab [glitch hop, new tribe, world fusion] 50
9 Nas [dirty south rap, east coast hip hop, gangster... 77
10 Widespread Panic [blues-rock, jam band, modern blues, roots roc... 56
11 2Pac [g funk, gangster rap, hip hop, pop rap, rap, ... 83
12 Phish [alternative rock, blues-rock, jam band, moder... 62
13 Koan Sound [brostep, catstep, chillstep, complextro, edm,... 45
14 Kendrick Lamar [hip hop, pop rap, rap, west coast rap] 95
15 A Tribe Called Quest [alternative hip hop, east coast hip hop, gang... 73
16 Jimi Hendrix [album rock, blues-rock, classic funk rock, cl... 77
17 Robert Glasper Experiment [deep indie r&b, indie jazz, indie r&b, neo soul] 55
18 Justice [alternative dance, dance-punk, electro house,... 67
19 Guns N' Roses [album rock, alternative rock, classic rock, g... 83

Visualization

In [3]:
import plotly
import plotly.plotly as py
import plotly.graph_objs as go

# use plotly API access key
plotly.tools.set_credentials_file(username='*******', api_key='***************;)
In [4]:
data = [
    go.Bar(
        x=df['Artists'], # assign x as the dataframe column 'x'
        y=df['Popularities']
    )
]

# IPython notebook
py.iplot(data, filename='pandas-bar-chart')
Out[4]:

Music Mainstream Meter

In [5]:
mmm = df['Popularities'].mean()
mmm
Out[5]:
68.900000000000006

Determining the number of artists with genres containing 'rock' in them

Loop through Genre column and track 'rock'

In [6]:
def chief_string(genre):
    if 'rock' in genre:
        return True
    else:
        return False
In [7]:
df['Genres'].apply(lambda x: chief_string(x))
Out[7]:
0      True
1      True
2     False
3     False
4      True
5      True
6     False
7     False
8     False
9     False
10    False
11    False
12     True
13    False
14    False
15    False
16     True
17    False
18    False
19     True
Name: Genres, dtype: bool
In [8]:
sum(df['Genres'].apply(lambda x: chief_string(x)))
Out[8]:
7
In [9]:
result = df['Genres'].apply(lambda x: chief_string(x))
new = df[result][['Artists']]
In [10]:
new = df[result]
new.reset_index(inplace=True)
new
Out[10]:
index Artists Genres Popularities
0 0 Metallica [alternative metal, alternative rock, hard roc... 86
1 1 Red Hot Chili Peppers [alternative rock, funk metal, funk rock, mode... 88
2 4 Neil Young [album rock, classic rock, mellow gold, perman... 74
3 5 Grateful Dead [blues-rock, classic rock, folk rock, jam band... 72
4 12 Phish [alternative rock, blues-rock, jam band, moder... 62
5 16 Jimi Hendrix [album rock, blues-rock, classic funk rock, cl... 77
6 19 Guns N' Roses [album rock, alternative rock, classic rock, g... 83
In [11]:
import cufflinks as cf

cf.set_config_file(world_readable=True,offline=False)
pie=cf.datagen.pie()
In [12]:
# pie.iplot(kind='pie',labels=new['Artists'],values=new['Popularities'])
In [13]:
def chief_string(genre):
    if 'classic rock' in genre:
        return True
    else:
        return False


result = df['Genres'].apply(lambda x: chief_string(x))
classicRock = df[result][['Artists']]
classicRock
Out[13]:
Artists
4 Neil Young
5 Grateful Dead
16 Jimi Hendrix
19 Guns N' Roses
In [14]:
def chief_string(genre):
    if 'alternative rock' in genre:
        return True
    else:
        return False
In [15]:
alternativeRock = df[result][['Artists']]
alternativeRock
Out[15]:
Artists
4 Neil Young
5 Grateful Dead
16 Jimi Hendrix
19 Guns N' Roses

Unforturnately, the different types of rock are associated with the same artists.

In [16]:
data = pd.read_json('/Users/troy/Github/courses/DANTH/myTopTracks.json')
df = data['items']
name = []
pop = []
ids = []
for item in data['items']:
    name.append(item['name'])
    pop.append(item['popularity'])
    ids.append(item['id'])

columns = {'Tracks': name,'Popularities': pop, 'ID': ids}
tracks = pd.DataFrame(data=columns)
tracks
Out[16]:
ID Popularities Tracks
0 28w7CPWQCqQxt0YCzDN75b 51 Quixoticelixer - Bonus Track
1 1HOrB4LPlKBC9tyX0bPNjs 31 Harvester of Sorrow (Live in Mexico City)
2 1XxCiPw07rd1ytYnE7SoJI 60 Road Trippin'
3 0X7rrzflUKygU7ROPBvRKM 45 Coffee and Cigarettes
4 1Ug98CRiQYu7wiSym4uLY4 61 Sad But True
5 1swp88wQzC976ubadoAqEZ 36 Hollywood Surf Club (feat. Mr. Talkbox)
6 0aGQHMr7bc23Y9Ts84ffop 64 Around The World
7 49rcSzMayJhIwGf0CJCaxJ 59 Wherever I May Roam
8 6hJzdYks1993CRwpSqePxs 29 The Reflection (feat. Sasha Rose, Dakini Star ...
9 1Se0r96r0gnqg67kJPmESc 61 Parallel Universe
10 2KNAEqEYvrplO3S3J2oVG5 29 Cheese
11 49colNVoLFcMszng9O8w4h 54 This Velvet Glove
12 5f2CgxUSiRtuEMcY6mmAW4 42 Kaleidoscope
13 1G391cbiT3v3Cywg8T7DM1 74 Scar Tissue
14 5BIMPccDwShpXq784RJlJp 72 Enter Sandman
15 3BW34zpQNgRUwuQ4eUyzMN 46 Fat Dance - Bonus Track
16 3M7K9ug8uPfZ5u6Qlgcs15 27 Brand New Angel
17 64BbK9SFKH2jk86U3dGj2P 77 Otherside
18 10igKaIKsSB6ZnWxPxPvKO 68 Nothing Else Matters
19 1jDOlilttG1vKtZLfwFRZD 54 Papa Was A Rollin' Stone
In [17]:
left = tracks[['ID','Tracks']].head(10)
left
Out[17]:
ID Tracks
0 28w7CPWQCqQxt0YCzDN75b Quixoticelixer - Bonus Track
1 1HOrB4LPlKBC9tyX0bPNjs Harvester of Sorrow (Live in Mexico City)
2 1XxCiPw07rd1ytYnE7SoJI Road Trippin'
3 0X7rrzflUKygU7ROPBvRKM Coffee and Cigarettes
4 1Ug98CRiQYu7wiSym4uLY4 Sad But True
5 1swp88wQzC976ubadoAqEZ Hollywood Surf Club (feat. Mr. Talkbox)
6 0aGQHMr7bc23Y9Ts84ffop Around The World
7 49rcSzMayJhIwGf0CJCaxJ Wherever I May Roam
8 6hJzdYks1993CRwpSqePxs The Reflection (feat. Sasha Rose, Dakini Star ...
9 1Se0r96r0gnqg67kJPmESc Parallel Universe
In [19]:
data = pd.read_json('/Users/troy/Github/courses/DANTH/top10TrackFeatures.json')
data


da = []
val = []
loud = []
energies = []
ids = []
for track in data['audio_features']:
    da.append(track['danceability'])
    val.append(track['valence'])
    loud.append(track['loudness'])
    energies.append(track['energy'])
    ids.append(track['id'])

columns = {'ID': ids,'Dance Ability': da, 'Valence': val, 'Loudness': loud, 'Energy': energies}
right = pd.DataFrame(data=columns)
right
Out[19]:
Dance Ability Energy ID Loudness Valence
0 0.535 0.831 28w7CPWQCqQxt0YCzDN75b -4.869 0.8340
1 0.305 0.976 1HOrB4LPlKBC9tyX0bPNjs -6.593 0.0676
2 0.243 0.470 1XxCiPw07rd1ytYnE7SoJI -8.194 0.3930
3 0.906 0.542 0X7rrzflUKygU7ROPBvRKM -8.146 0.6530
4 0.632 0.845 1Ug98CRiQYu7wiSym4uLY4 -6.336 0.4330
5 0.601 0.688 1swp88wQzC976ubadoAqEZ -4.178 0.2350
6 0.504 0.960 0aGQHMr7bc23Y9Ts84ffop -1.464 0.7740
7 0.554 0.816 49rcSzMayJhIwGf0CJCaxJ -7.871 0.3620
8 0.936 0.237 6hJzdYks1993CRwpSqePxs -11.230 0.6290
9 0.343 0.983 1Se0r96r0gnqg67kJPmESc -2.359 0.1860
In [20]:
main = pd.merge(left,right, on=left['ID'])
main
Out[20]:
ID_x Tracks Dance Ability Energy ID_y Loudness Valence
0 28w7CPWQCqQxt0YCzDN75b Quixoticelixer - Bonus Track 0.535 0.831 28w7CPWQCqQxt0YCzDN75b -4.869 0.8340
1 1HOrB4LPlKBC9tyX0bPNjs Harvester of Sorrow (Live in Mexico City) 0.305 0.976 1HOrB4LPlKBC9tyX0bPNjs -6.593 0.0676
2 1XxCiPw07rd1ytYnE7SoJI Road Trippin' 0.243 0.470 1XxCiPw07rd1ytYnE7SoJI -8.194 0.3930
3 0X7rrzflUKygU7ROPBvRKM Coffee and Cigarettes 0.906 0.542 0X7rrzflUKygU7ROPBvRKM -8.146 0.6530
4 1Ug98CRiQYu7wiSym4uLY4 Sad But True 0.632 0.845 1Ug98CRiQYu7wiSym4uLY4 -6.336 0.4330
5 1swp88wQzC976ubadoAqEZ Hollywood Surf Club (feat. Mr. Talkbox) 0.601 0.688 1swp88wQzC976ubadoAqEZ -4.178 0.2350
6 0aGQHMr7bc23Y9Ts84ffop Around The World 0.504 0.960 0aGQHMr7bc23Y9Ts84ffop -1.464 0.7740
7 49rcSzMayJhIwGf0CJCaxJ Wherever I May Roam 0.554 0.816 49rcSzMayJhIwGf0CJCaxJ -7.871 0.3620
8 6hJzdYks1993CRwpSqePxs The Reflection (feat. Sasha Rose, Dakini Star ... 0.936 0.237 6hJzdYks1993CRwpSqePxs -11.230 0.6290
9 1Se0r96r0gnqg67kJPmESc Parallel Universe 0.343 0.983 1Se0r96r0gnqg67kJPmESc -2.359 0.1860
In [21]:
import plotly.plotly as py
import plotly.graph_objs as go


trace1 = go.Bar(
    x=main['Tracks'],
    y=main['Dance Ability'],
    name='Dance Ability'
)
trace2 = go.Bar(
    x=main['Tracks'],
    y=main['Energy'],
    name='Energy'
)
trace3 = go.Bar(
    x=main['Tracks'],
    y=main['Valence'],
    name='Valence'
)
trace4 = go.Bar(
    x=main['Tracks'],
    y=main['Loudness'],
    name='Loudness'
)

# plot_data = [trace1, trace2, trace3, trace4]
plot_data = [trace1, trace2, trace3]
layout = go.Layout(
    barmode='group'
)

fig = go.Figure(data=plot_data, layout=layout)
py.iplot(fig, filename='grouped-bar')
Out[21]: