I have a data set that is looks like this, and there are instances in which there are multiple duplicates (ex.Gone Girl is repeated twice). I am not proficient at Python so I don't have any code at the moment and have tried looking all over stackoverflow.
But my objective is:
Appreciate any help
| Book Name | Author | User Rating |
|---|---|---|
| The Casual Vacancy | JK Rowling | 4.9 |
| Cabin Fever (Diary of a Wimpy Kid, Book 6) | Jeff Kinney | 3.9 |
| Harry Potter | JK Rowling | 4.7 |
| Cabin Fever (Diary of a Wimpy Kid, Book 6) | Jeff Kinney | 4.4 |
| Gone Girl | Gillian Flynn | 4.0 |
| Gone Girl | Gillian Flynn | 4.0 |
| The Girl on the Train) | Paula Hawkins | 4.1 |
I'm not sure how you'd like the output format, but here is a way to drop duplicate books by the same author, and return the average score of the author after duplicate removal using the pandas library:
import pandas as pd
df = pd.read_csv('mydata.txt', sep='\t') # use this if it is a tab delimited text file
df = pd.read_csv('mydata.csv') # use this if it is a comma separated value file
subset = df.drop_duplicates(subset=['Book Name', 'Author']).groupby('Author').agg({"User Rating": "mean"})
print(subset)
outputs:
User Rating
Author
Gillian Flynn 4.0
Feff Kinney 3.9
JK Rowling 4.8
Paula Hawkins 4.1
Explanations:
First, I am creating a pandas dataframe using the pandas library. If the data is in tab delimited text format, use the first line df = pd.read_csv('mydata.txt', sep='\t') to read in the data. If the data is in comma separated value format, use the second line df = pd.read_csv('mydata.csv') to read in the data. This creates the dataframe.
Then, df.drop_duplicates drops duplicate entries in a dataframe. If you select a subset it will look for duplicates of the subset passed. So in this case, I passed a list of two columns where I wanted to drop duplicates ['Book Name', 'Author']. When you pass multiple columns, both of them have to be identical for it to be counted as a duplicate.
Then, I groupby the 'Author' column which will perform an agg or aggregation, to get the mean of the User Rating column, for each 'Author'.
I would use df.groupby(...).mean() 2 times, to be consistent if, for an author, there are multiple books of which some have multiple ratings. But, the specifications may differ.
Calculate the mean note for each 'Author', 'Book Name' couple
If for a couple the User Ratings are the same, that waste some ressources with no arm.
Calculate the mean note by author
the code is :
df.groupby(['Author', 'Book Name']]).mean().groupby(['Author']).mean()
with value :
User Rating
Author
Gillian Flynn 4.00
JK Rowling 4.80
Jeff Kinney 4.15
Paula Hawkins 4.10