[Python] Pandas. TypeError: can only concatenate str (not "int") to str

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    I've been given a problem to solve which I seem to do great.

    The problem is: Print the average amount of reviews among books with a rating above 4 and more than 1000 total ratings, and also show ones with rating in the range of 2-3 and total ratings below 500

    Here is my code:

    import pandas as pd

    df = pd.read_csv('data/GoodReads_100k_books.csv')

    above = df[(df['rating'] > 4) & (df['totalratings'] > 1000)].mean()

    below = df[(2 <= df['rating'] <= 3) & (df['totalratings'] < 500)].mean()

    print(above)

    print(below)


    To me it seems fine but it returns this error:

    TypeError Traceback (most recent call last)
    Cell In[15], line 5
    1 import pandas as pd
    3 df = pd.read_csv('data/GoodReads_100k_books.csv')
    ----> 5 above = df[(df['rating'] > 4) & (df['totalratings'] > 1000)].mean()
    7 below = df[(2 <= df['rating'] <= 3) & (df['totalratings'] < 500)].mean()
    9 print(above)

    TypeError: can only concatenate str (not "int") to str


    I've asked TabnineAI what is wrong with the code and why does it return this error.

    It said that there is nothing wrong with the code and the concatenation has nothing to do with my code.

    How can I solve this?

    Continue reading...

Compartilhe esta Página