iTunes Library analysis using Python

  • Windows: C:\Users\<username>\Music\iTunes
  • File →Export library
Fig 1: A snapshot of Library.xml

Python Script

  • <key>Tracks<key>
  • <dict>
Fig 2: A snapshot of collapsed Library XML

Importing Libraries

import pandas as pd
import xml.etree.ElementTree as ET ## XML parsing

Reading Library file

lib = r'C:\Users\<username>\Music\iTunes\Library.xml'

Parsing the library file

tree = ET.parse(lib)
root = tree.getroot()
main_dict=root.findall('dict')
for item in list(main_dict[0]):
if item.tag=="dict":
tracks_dict=item
break
tracklist=list(tracks_dict.findall('dict'))
Fig 3: tracklist variable
  • podcast :- list of XML elements under podcast
  • purchased:- list of XML elements purchased from iTunes
  • apple_music:- list of XML elements added to library through Apple Music subscription
Fig 4: how apple_music variable looks like
print (“Number of tracks under Podcast: “,str(len(podcast)))
print (“Number of tracks Purchased: “,str(len(purchased)))
print (“Number of Music added thought Apple Music subscription: “,str(len(apple_music)))
  • Column names can be obtained from the content of <key> tag. (Please refer Fig 1: A snapshot of Library.xml)
  • podcast_cols :- dictionary of all column names from podcast
  • purchased_cols:- dictionary of all column names from purchased Music
  • apple_music_cols:- dictionary of all column names from Subscrip
Fig 5: dictionary of column names
Fig 6: Apple Music Datafram
Fig 7: A snapshot of the Data

Analysis

Artist Word Cloud

df = df_songs.groupby([‘artist’])[‘play_count’].sum().reset_index()
df[‘desc’] = (df[‘artist’]+’ ‘)*df[‘play_count’]
text = “ “.join(item for item in df.desc)
wordcloud = WordCloud(background_color="white",collocations=False).generate(text)
plt.figure(figsize = (8, 8), facecolor = None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad = 0)
plt.show()
Fig 8: Artist Word cloud

Genre Distribution

Fig 8: Genre Distribution

Top Artists by Play Count

Fig 9: Popular Aritists

Track Distribution by Era

Fig 10: Tracks by Era

Most Played & Skipped Songs

Recommended Songs

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store