• Register
0 votes

I am trying to create a rating data frame for some football teams. To rate the teams I am using elo rating system  :


My data frame consists of pre-match ratings of home and away teams and it gets updated after every match.

My data frame code is:

for index in df.index:

    counter = counter + 1

    if df.at[index,'updated'] == 2: 

            all_home_fixtures = df.date_rank[df['localteam_id'] == df.at[index,'localteam_id']]
            next_home_fixture =all_home_fixtures[all_home_fixtures>df.at[index,'date_rank']].min()
            next_home_index = df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])].index.item()
        except ValueError:
            print('ERROR 1 at' + str(index))
            df.at[index,'updated'] = 4

            all_away_fixtures = df.date_rank[df['visitorteam_id'] == df.at[index,'visitorteam_id']]
            next_away_fixture = all_away_fixtures[all_away_fixtures > df.at[index,'date_rank']].min()
            next_away_index = df[(df['date_rank'] == next_away_fixture) & (df['visitorteam_id'] == df.at[index,'visitorteam_id'])].index.item()
        except ValueError:
            print('ERROR 2 at' + str(index))
            df.at[index,'updated'] = 4

str(df.at[index,'date_rank']) + '; Next home date rank: ' + str(df.at[next_home_index,'date_rank']) + '; Next away date rank: ' + str(df.at[next_away_index,'date_rank']))

        df.at[next_home_index, 'home_elo'] = update_elo(df.at[index,'home_elo'],df.at[index,'away_elo'],df.at[index,'actual_score'])
        df.at[next_away_index, 'away_elo'] = update_elo(df.at[index,'away_elo'],df.at[index,'home_elo'],1 - df.at[index,'actual_score']) # Swap function inputs for away team

        df.at[next_home_index, 'updated'] = df.at[next_home_index, 'updated'] + 1
        df.at[next_away_index, 'updated'] = df.at[next_away_index, 'updated'] + 1

        df.at[index,'updated'] = 3

But after working in some of the rows it gives me an error like :

ValueError: can only convert an array of size 1 to a python scalar

Can any one help me to find what the problem really is and help me to solve the problem.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes

Please check if all your indexes are unique or not. Some of your index calls may return multiple values if you have any index that is working variously.

Try this in your code:

df.index.nunique() == df.index.shape[0]

 Your pd.Series.item may require one item in the Series to return a scalar.

If a series is with length zero it will return a ValueError.


df[(df['date_rank'] == next_home_fixture) & (df['localteam_id'] == df.at[index,'localteam_id'])]


11 6 4
34,950 points

Related questions

0 votes
1 answer 8 views
Problem: I have large json data that reads into a python dataframe and creates a list of dicts for each line. I need to convert it to a different data format. The data format is as follows: { "data": [{ "item": [{ "value": 0, "type": "a" }, { " ... ) df = df_formatted.fillna(0) The number of items in a list is often in the thousands. Are there pointers or examples on how to do this efficiently?
asked Dec 23, 2020 sasha 5.3k points
0 votes
1 answer 6 views
Problem: I tried to combine two lists: Names and Age. But I wanted to do this by adding their index [i + 1] to a different list each time. So instead of ['John', '17', 'Mike', '21'], my goal was for each pair to have its own index and be a list item. ... the path in the attached code, I achieve what I am trying to do. Can anyone explain why this works? I couldn't catch it. Thank you in advance.
asked Dec 24, 2020 sasha 5.3k points
0 votes
1 answer 25 views
Problem: I'm trying to run a code: f=np.loadtxt('Single Small Angle 1.txt',unpack=True,skiprows=2) g=np.loadtxt('Single Small Angle 5.txt',unpack=True,skiprows=2) x = f-g[:,:11944] t=range(len(x)) m=math.log10(abs(x)) np.polyfit(t,m)plt.plot(t,abs(x)) plt.show() m=math.log10(abs(x)) TypeError: only length-1 arrays can be converted to Python scalars. I'm just not sure how to fix my problem.
asked Nov 5, 2020 Han Li 710 points
1 vote
1 answer 54 views
Problem: I am doing a online course on python. I got an assginement from there. So I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return ... .int(a) TypeError: only size-1 arrays can be converted to Python scalars Do anyone know why this error is happening, please help.
asked Apr 3, 2020 LizzyM 6.1k points
0 votes
1 answer 309 views
I was trying to write a programm using numpy and matplotlib. I am new to python programming. The code I wrote is: import numpy import matplotlib.pyplot def ThisFunction(a): return numpy.int(a) a = numpy.arange(3, 13.5, 0.7) matplotlib.pyplot.plot(a, ... 4, in ThisFunction return numpy.int(a) TypeError: only size-1 arrays can be converted to Python scalars how can I solve this problem?
asked Mar 15, 2020 LizzyM 6.1k points
2 votes
2 answers 595 views
Problem: I wrote the following code in python using numpy. But it gives me some error message. The code I wrote is: import numpy as np arrayone = np.array(['Human', 'Birds', 'Groots', 'Oats']) arraytwo = np.array(['Beach', 'Toast']) arraythree ... concatenate TypeError: only integer scalar arrays can be converted to a scalar index Can anyone sort out this error and solve this. Thanks in advance.
asked May 6, 2020 samhaz 5k points
0 votes
1 answer 119 views
Here's what I've got. I have two data frames. One is a set of financial data that already exists in the system and another set that has some that may or may not exist in the system. I need to find the difference and add the ... pandas\core\frame.py", line 3571, in _compare_frame raise ValueError('Can only compare identically-labeled ' ValueError: Can only compare identically-labeled DataFrame
asked Oct 28, 2020 psandprop 2.4k points