Skip to content

Instantly share code, notes, and snippets.

@SushantKalambe
Last active July 7, 2021 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SushantKalambe/230ce4036237b43108bfe16d7277c419 to your computer and use it in GitHub Desktop.
Save SushantKalambe/230ce4036237b43108bfe16d7277c419 to your computer and use it in GitHub Desktop.
Function to compare count of reviews of electronic products from Ecommerce websites by date.
#import libraries
import numpy as np
import pandas as pd
import datetime
import matplotlib.pyplot as plt
import json
from matplotlib.pyplot import figure
# import json files containing reviews
# paste path of files
data=[open(r'.....\applewatch6-2960-reviews.json','rb'),
open(r'.....\applewatchSE-1709-reviews.json','rb'),
open(r'.....\fitbitversa2-5000-reviews.json','rb'),
open(r'.....\fitbitversa3-2494-reviews.json','rb'),
open(r'.....\samsunggalaxy2-2711-reviews.json','rb')]
# This function returns a list of dataframes with date and count of reviews sorted based on their dates
def plot_data(data):
data_df=[]
for i in data:
x=json.load(i)
x=pd.DataFrame(pd.DataFrame(x.items())[1][14])
x['date'] = pd.to_datetime(x['date'])
month_year=[]
for j in x["date"]:
month_year.append(j.strftime("%m-%Y"))
x['month_year'] = month_year
x['month_year'] = pd.to_datetime(x['month_year'])
data_df.append(pd.DataFrame(x['month_year'].value_counts()).reset_index().rename(columns={"index" : "mon_year",
'month_year':'counts'}).sort_values(by='mon_year'))
return data_df
#Run the function
data_df=plot_data(data)
#create new list excluding the reviews from unwanted month.Here Unwanted month is 7th month as there are very less reviews in 7th month
new_data_df=[]
for i in range(0,len(data_df)):
new_data_df.append(data_df[i][data_df[i]['mon_year'].dt.month != 7])
# Plots
figure(figsize=(18, 10), dpi=80)
for i in new_data_df:
plt.plot(i['mon_year'], i['counts'],marker='o')
plt.legend(['Apple','Bose','Jabra','Samsung','Powerbeats'])
plt.title('count Vs Year')
plt.xlabel('Year')
plt.ylabel('count Rate')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment