# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
# %matplotlib inline

import pandas as pd

r = pd.read_csv( 'ratings.csv' )
tr = pd.read_csv( 'to_read.csv' )
b = pd.read_csv( 'books.csv' )

t = pd.read_csv( 'tags.csv' )
bt = pd.read_csv( 'book_tags.csv')

# -

# ### Ratings

r.head()

# Ratings are sorted chronologically, oldest first.

len(r)

r.rating.hist( bins = 5 )

# It appears that 4 is the most popular rating. There are relatively few ones and twos.

# ### To read
# These are the books users marked "to read".
#

tr.head()

len(tr)

# Most books have been marked to read by somebody. Majority of the users have some books marked to read.

len(tr.book_id.unique())

len(tr.user_id.unique())

# ### Books

b.head()

# Books are sorted by their popularity, as measured by number of ratings (overall, not in this dataset).

len(b)

# ### User and book IDs

r.user_id.max()

r.book_id.max()

# The IDs are contiguous.

assert( len( r.user_id.unique()) == r.user_id.max())
assert( len( r.book_id.unique()) == r.book_id.max())

# ### Reviews per book

# Most books have a few hundred reviews, but some have as few as eight.

reviews_per_book = r.groupby( 'book_id' ).book_id.apply( lambda x: len( x ))
reviews_per_book.describe()

reviews_per_book.sort_values().head( 10 )

# ### Reviews per user

# All users have at least 19 reviews.

reviews_per_user = r.groupby( 'user_id' ).user_id.apply( lambda x: len( x ))
reviews_per_user.describe()

reviews_per_user.sort_values().head( 10 )

# ### Tags

t.head()

len(t)

bt.head()

# Tag applications are sorted by goodreads_book_id ASC and by count DESC.

len(bt)

# Let us merge tag names into tag applications.

bt = bt.merge( t, on = 'tag_id' )

# Why don't we merge book titles for good measure.

bt = bt.merge( b[[ 'goodreads_book_id', 'title']], on = 'goodreads_book_id' )

# It appears that there are some negative tag counts. Let's fix this.

bt['count'].describe()

bt.loc[ bt['count'] < 0, 'count'] = 0

# Sample some popular book tags.

bt.sample( 10, weights = 'count')

# These are the most popular tags:

tag_counts = bt.groupby( 'tag_name' ).tag_name.count().sort_values( ascending = False )
tag_counts.head( 20 )



StarcoderdataJupyterScriptsDedupFiltered