Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

503
Views
Python: Return Average on unique values based on multiple columns

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:

  1. Remove all the duplicates and count how many times the author has written a book (ex. JK Rowling has written 2 different books and the other have all written 1)
  2. Based on the unique books, what is the average rating of the author (ex. JK Rowlings would be (4.9+4.7)/2 = 4.55

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
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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'.

over 4 years ago · Santiago Trujillo Report

0

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.

  1. 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.

  2. 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
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!