Table of Contents
The TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” occurs when attempting to perform a mathematical operation between a string and an integer. This error can be fixed by ensuring that both operands are of the same data type, either by converting the string to an integer or vice versa. Additionally, it is important to check the data types of all variables and inputs to avoid this error.
Fix: TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’
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 columnprint(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: objectNotice 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:
Cite this article
stats writer (2024). Can the TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” be fixed?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/can-the-typeerror-unsupported-operand-types-for-str-and-int-be-fixed/
stats writer. "Can the TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” be fixed?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/can-the-typeerror-unsupported-operand-types-for-str-and-int-be-fixed/.
stats writer. "Can the TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” be fixed?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/can-the-typeerror-unsupported-operand-types-for-str-and-int-be-fixed/.
stats writer (2024) 'Can the TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” be fixed?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/can-the-typeerror-unsupported-operand-types-for-str-and-int-be-fixed/.
[1] stats writer, "Can the TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” be fixed?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. Can the TypeError “unsupported operand type(s) for -: ‘str’ and ‘int'” be fixed?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
