In [1]:
from django.core.management import call_command
from django.db import connection
from graphviz import Source
from IPython.display import Image
import pandas as pd
pd.options.display.max_rows = 10
Another way to get the output of a management command:
In [2]:
%%capture dot
call_command('graph_models', 'music')
In [3]:
Source(dot)
Out[3]:
In [4]:
songs = pd.read_sql("""
SELECT
id AS song_id
, title AS song_title
, track AS release_track
, release_id
FROM music_song
""", connection)
songs.head()
Out[4]:
In [5]:
releases = pd.read_sql("""
SELECT
id AS release_id
, title AS release_title
, date AS release_date
FROM music_release
""", connection)
releases.head()
Out[5]:
Join the DataFrame
s on the common release_id
column:
In [6]:
song_releases = songs.merge(releases).set_index('song_id')
song_releases.head()
Out[6]:
In [7]:
release_tracks = song_releases.groupby('release_title')['song_title'].count()
release_tracks
Out[7]:
In [8]:
release_tracks.describe()
Out[8]:
In [9]:
song_releases.to_csv('song-releases.csv')
!head song-releases.csv
In [10]:
pd.read_csv('song-releases.csv', index_col='song_id').head()
Out[10]:
In [ ]: