Percentage of Video Game Genres in Spyder

Asked

Viewed 52 times

0

I want to show the percentage of various genres in my code with the date set and it is giving a following error:

Valueerror: Lengths must match to compare

The part that has the error in my code is like this, help me find the error

base = pd.read_csv ('gamestest.csv')
#base.Platform (a tabela excel possui uma coluna Platform)

porcen = base.DataFrame(base.Genre.unique())
totalPorcPlat = len(base)

for porc in porcen:
    contagemPorcen = base[base.Genre]==porcen].shape(0)
    print ('{}: {:0.2f}%'.format(porcen, contagemPorcen * 100/ totalPorcPlat))
  • I think the confusion is in Platform and Platforms. If it’s not for Platforms in Platform:, the last two lines should use Platforms plural, and not Platform. Check the indentation too, I think the code was copied wrong in the question.

  • But in my algorithm there is a table called Platform

  • base[base['Platform']==Platforms].shape[0] and .format(Platforms, .... I meant the variable, not the column.

  • It didn’t work, still returns the same error.

  • Hey @Alexciuffa I had to change some parts of my code for better understanding, the change I made is the percentage of video game genre.

1 answer

0

Given a list, lista = ['plataforma 1', 'plataforma 1', 'plataforma 2']

When we do:

for valor in lista:
    print(valor)

The exit is:

plataforma 1
plataforma 1
plataforma 2

In your code is:

for porc in porcen:
    contagemPorcen = base[base['Platform']==porcen].shape(0)
    print ('{}: {:0.2f}%'.format(porcen, contagemPorcen * 100/ totalPorcPlat))

But the correct one would be to use the variable porc (list value) instead of porcen (entire list):

for porc in porcen:
    contagemPorcen = base[base['Platform']==porc].shape(0)
    print ('{}: {:0.2f}%'.format(porc, contagemPorcen * 100/ totalPorcPlat))

Still, there is another error. Instead of .shape(0), the correct is .shape[0].

EDIT

To list in order, you need to store all values, sort and then show.

lista_porcentagens = []
for porc in porcen:
    contagemPorcen = base[base['Platform']==porc].shape[0]
    lista_porcentagens.append((contagemPorcen * 100/ totalPorcPlat, porc))

#ordenar a lista do maior para o menor    
lista_porcentagens.sort(reverse=True)
#printar os valores
for porcentagem, plataforma in lista_porcentagens:
    print ('{}: {:0.2f}%'.format(plataforma, porcentagem))
  • Thank you, now I understand the error and is running. Only on the line porcen = base.Genre.unique() when I click on the Spyder variable explorer gives the following message: Matrix Explorer: Matrix Object is not currently supported.

  • How to present the percentages from the highest to the lowest (decreasing)?

  • I edited the answer with this addendum.

  • ((contagemPorcen * 100/ totalPorcPlat, porc)) what is this calculation I don’t understand

  • for porcentagem, plataforma in lista_porcentagens: print ('{}: {:0.2f}%'.format(plataforma, porcentagem)) is not running

  • Where did this variable come from porcentagem?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.