-1

Here is the code and the output. I assume is it about the score not being int, but not sure how to convert in this case

The code and the output

df.index = df.columns
rows = []
for i in df.index:
    for c in df.columns:
        if i == c: continue
        score = df.ix[i, c]
        score = [int(row) for row in score.split('-')]
        rows.append([i, c, score[0], score[1]])
df = pd.DataFrame(rows, columns = ['home', 'away', 'home_score', 'away_score'])
df.head()
5
  • 1
    You shouldn't paste screenshots of code but the actual code text itself Commented Apr 20, 2021 at 2:57
  • 1
    @Buckeye14Guy he is using score.split("-") though Commented Apr 20, 2021 at 3:02
  • @Buckeye14Guy, thanks, still an error. slightly different though, just '-' left in the tail of the error Commented Apr 20, 2021 at 3:04
  • The only thing I can think of is that the character is not really '-'... Commented Apr 20, 2021 at 3:05
  • @hobbs answer below explains what might be happening. Simple solution is this: [int(row) for row in score if row.isnumeric()] Commented Apr 20, 2021 at 3:07

3 Answers 3

3

You're splitting on "-" (U+0020 HYPHEN-MINUS), but your data is using some other character... it's hard to say since you provided a picture of the error instead of the actual error, but it's probably "–" (U+2013 EN DASH). Fix your split to use the character that actually occurs in the input.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you should just do

score = [int(row) for row in score if row.isnumeric()]

Take advantage of the .isnumeric() method of strings.

P.S. You should not be using .ix method with Pandas. I am pretty sure it is deprecated.

Comments

-1

The screenshot you posted is identifying the issue. You are trying to call int() on the str 0-3. This can't be done. From my terminal

In [1]: int('0-3')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-c6bc87cd2bc7> in <module>
----> 1 int('0-3')

ValueError: invalid literal for int() with base 10: '0-3'

Looking at your dataframe it looks like you have a lot of data that can't be turned into an int as is

5 Comments

he is using score.split("-") though
Not correctly. The the error output clearly shows he's trying to turn '0-3' into an int. He's not actually splitting that string. The row variable there isn't the string from the actual row
yeah I understand, and he knows that. His question is how to convert it, not what the problem is.
Not sure that that's the case based on the question title...Also if he wants to know how to convert 0-3 to an integer he needs to give more instruction on what he expects that behavior to be. The code he's using is overwriting the same df variable and he hasn't pasted in any data for anyone else to reproduce it
@sedavidw Look at Hobbs' answer. He got it right. OP's code is correct. He is just not splitting by the correct hyphen character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.