Home » How to Fix: TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’

How to Fix: TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’

by Tutor Aspire

One error you may encounter when using Python is:

TypeError: unsupported operand type(s) for -: 'str' and 'int'

This error occurs when you attempt to perform subtraction with a string variable and a numeric variable.

The following example shows how to address this error in practice.

How to Reproduce the Error

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points_for': ['18', '22', '19', '14', '14', '11', '20', '28'],
                   'points_against': [5, 7, 17, 22, 12, 9, 9, 4]})

#view DataFrame
print(df)

  team points_for  points_against
0    A         18               5
1    B         22               7
2    C         19              17
3    D         14              22
4    E         14              12
5    F         11               9
6    G         20               9
7    H         28               4

#view data type of each column
print(df.dtypes)

team              object
points_for        object
points_against     int64
dtype: object

Now suppose we attempt to subtract the points_against column from the points_for column:

#attempt to perform subtraction
df['diff'] = df.points_for - df.points_against

TypeError: unsupported operand type(s) for -: 'str' and 'int'

We receive a TypeError because the points_for column is a string while the points_against column is numeric.

In order to perform subtraction, both columns must be numeric.

How to Fix the Error

To resolve this error, we can use .astype(int) to convert the points_for column to an integer before performing the subtraction:

#convert points_for column to integer
df['points_for'] = df['points_for'].astype(int)

#perform subtraction
df['diff'] = df.points_for - df.points_against

#view updated DataFrame
print(df)

  team  points_for  points_against  diff
0    A          18               5    13
1    B          22               7    15
2    C          19              17     2
3    D          14              22    -8
4    E          14              12     2
5    F          11               9     2
6    G          20               9    11
7    H          28               4    24

#view data type of each column
print(df.dtypes)

team              object
points_for         int32
points_against     int64
diff               int64
dtype: object

Notice that we don’t receive an error because both columns we used for the subtraction are numeric columns.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes

You may also like