Search code examples
pythonpandascomparedata-scienceseries

How can we compare values of a data frame to a series?


I want to find a book with most sold score. For this, I have created a series (unique_books) with names of all the books. Next, I want to compare its values with the books column (Book Name) but it is keep generating errors. I an a newbie so this question may feel dumb to you. Please excuse my silliness and do help me sort it out. Any help would be appreciated.

unique_books = pd.Series(unique_books) #converting ndarray to pd series
type(unique_books)
bestselling = pd.Series(unique_books, dtype="int")
type(bestselling)

counter = 0
bestselling_book = 0
for i in range(0, len(df['Book Name'])):
    
    if df[df["Book Name"]].equals(unique_books[unique_books[i]]):
        counter += 1
    
    if bestselling_book < counter:
        bestselling_book = counter
        
print ("The best selling book is " + str(bestselling_book))

Solution

  • Based on your comment:

    df = pd.DataFrame({'books':['A', 'A', 'B', 'A']})
    df['books'].value_counts()
    

    output:

    enter image description here