-2

When I try to do the following, the subsequent error occurs.

    ranges = []
    a_values= []
    b_values= []
    
    for x in params:
        a=  min(fifa[params][x])
        a= a - (a*.25)
        
        b = max(fifa[params][x])
        b = b + (b*.25)
        
        ranges.append((a,b))
for x in range(len(fifa['short_name'])):
    if fifa['short_name'][x]=='Nunez':
        a_values = df.iloc[x].values.tolist()

Error Description

What does it mean? How do I solve this?

Thank you in advance

  • Please [don't post code/errors/data as images](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question)). Also, there's some trouble with the indentation of your code. – Grismar Sep 21 '22 at 21:39
  • this means on line 16 : ```fifa['short_name']``` is empty list so you can not access it's items – khaled koubaa Sep 21 '22 at 21:43
  • @khaledkoubaa that line will only get executed if `len(fifa['short_name'])` is greater than 0, since it sits within that for loop, so that can't be the case. (it's also not a list, but most likely a Series) – Grismar Sep 21 '22 at 21:44
  • Your code is also lacking the definition of `params` and `fifa`, which would be help provide a better answer. – Grismar Sep 21 '22 at 21:48
  • in line 16 try ```fifa['short_name'].iloc[x]``` instead of ```fifa['short_name'][x]``` – khaled koubaa Sep 21 '22 at 21:56

1 Answers1

0

The problem is on this line:

if fifa['short_name'][x]=='Nunez':
  • fifa['short_name'] is a Series;
  • fifa['short_name'][x] tries to index that series with x;
  • your code doesn't show it, but the stack trace suggests x is some scalar type;
  • pandas tries to look up x in the index of fifa['short_name'], and it's not there, resulting in the error.

Since the Series will share the index of the dataframe fifa, this means that the index x isn't in the dataframe. And it probably isn't, because you let x range from 0 upto (but not including) len(fifa).

What is the index of your dataframe? You didn't include the definition of params, nor that of fifa, but your problem is most likely in the latter, or you should loop over the dataframe differently, by looping over its index instead of just integers.

However, there's more efficient ways to do what you're trying to do generally in pandas - you should just include some definition of the dataframe to allow people to show you the correct one.

Grismar
  • 27,561
  • 4
  • 31
  • 54