<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Pivot review data in pandas
This notebook shows how data can be pivoted by python pandas to reveal insights into the behaviour of reviewers. The use case and data is from Mark Harwood's talk on entity-centric indexing.
An alternative version of this notebook uses the Elastic data frames to create the same results.
Step1:  Read data to pandas DataFrame
Step2:  Explore data
Step3:  Distribution of reviews (high number of five star ratings)
Step4:  Aggregate and Pivot data
Pivot data so we get summaries for each reviewer.
Step5:  Find 'haters'
Reviewers that give more than five zero star reviews to one vendor
Step6:  For example, reviewer 10392 gives 94 zero star reviews to vendor 122
Step7:  Find 'fanboys'
Reviewers that give more than five five star reviews to one vendor
Step8:  Reviewer 183751 gives 73 five star reviews to vendor 190
<ASSISTANT_TASK:>
Python Code:
import bz2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
Explanation: Pivot review data in pandas
This notebook shows how data can be pivoted by python pandas to reveal insights into the behaviour of reviewers. The use case and data is from Mark Harwood's talk on entity-centric indexing.
An alternative version of this notebook uses the Elastic data frames to create the same results.
End of explanation
csv_handle = bz2.open('./anonreviews.csv.bz2')
reviews = pd.read_csv(csv_handle)
reviews['date'] = pd.to_datetime(reviews['date'])
Explanation: Read data to pandas DataFrame
End of explanation
reviews.head()
reviews.describe()
reviews.info()
Explanation: Explore data
End of explanation
reviews.hist(column="rating", bins = 5)
plt.show()
#### Typically how many vendors does a reviewer review? (mainly one or two)
plt.plot(reviews.groupby('reviewerId')['vendorId'].nunique(), '.')
plt.xlabel('reviewerId')
plt.ylabel('dc(vendorId)')
plt.show()
Explanation: Distribution of reviews (high number of five star ratings)
End of explanation
aggregations = {
    'rating':'mean',
    'vendorId':'nunique',
    'reviewerId':'count'
}
grouped = reviews.groupby('reviewerId').agg(aggregations)
grouped.columns=['avg_rating', 'dc_vendorId', 'count']
grouped.head()
grouped.describe()
plt.rcParams["figure.figsize"] = (10,10)
scatter_matrix(grouped)
plt.show()
Explanation: Aggregate and Pivot data
Pivot data so we get summaries for each reviewer.
End of explanation
grouped[ 
    (grouped['dc_vendorId'] == 1) & 
    (grouped['count'] > 5) & 
    (grouped['avg_rating'] == 0)
].sort_values('count', ascending=False)
Explanation: Find 'haters'
Reviewers that give more than five zero star reviews to one vendor
End of explanation
reviews[reviews['reviewerId'] == 10392].head()
Explanation: For example, reviewer 10392 gives 94 zero star reviews to vendor 122
End of explanation
grouped[ 
    (grouped['dc_vendorId'] == 1) & 
    (grouped['count'] > 5) & 
    (grouped['avg_rating'] == 5) 
].sort_values('count', ascending=False)
Explanation: Find 'fanboys'
Reviewers that give more than five five star reviews to one vendor
End of explanation
reviews[reviews['reviewerId'] == 183751].head()
Explanation: Reviewer 183751 gives 73 five star reviews to vendor 190
End of explanation
<END_TASK>