Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

505
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda