I'm attempting to use Spyder to convert a table of temperature measurements from Fahrenheit to Celsius, but I'm getting this error: "unsupported operand type(s) for -: 'str' and 'int'"
I'm using this fuction:
for f in data:
c = round((f-32)/1.8,1)
print(f,c)
and this is the data:
YEARMODA TEMP MAX MIN AMPLITUDE
0 20160601 65.5 73.6 54.7 18.9
1 20160602 65.8 80.8 55.0 25.8
2 20160603 68.4 NaN 55.6 NaN
3 20160604 57.5 70.9 47.3 23.6
4 20160605 51.4 58.3 43.2 15.1
5 20160606 52.2 59.7 42.8 16.9
6 20160607 56.9 65.1 45.9 19.2
7 20160608 54.2 NaN 47.5 NaN
8 20160609 49.4 54.1 45.7 8.4
9 20160610 49.5 55.9 43.0 12.9
10 20160611 54.0 62.1 41.7 20.4
11 20160612 55.4 64.2 46.0 18.2
12 20160613 58.3 68.2 47.3 20.9
13 20160614 59.7 67.8 47.8 20.0
14 20160615 63.4 70.3 49.3 21.0
15 20160616 57.8 67.5 55.6 11.9
16 20160617 60.4 70.7 55.9 14.8
17 20160618 57.3 NaN 54.0 NaN
18 20160619 56.3 59.2 54.1 5.1
19 20160620 59.3 69.1 52.2 16.9
20 20160621 62.6 71.4 50.4 21.0
21 20160622 61.7 70.2 55.4 14.8
22 20160623 60.9 67.1 54.9 12.2
23 20160624 61.1 68.9 56.7 12.2
24 20160625 65.7 75.4 57.9 17.5
25 20160626 69.6 77.7 60.3 17.4
26 20160627 60.7 70.0 NaN NaN
27 20160628 65.4 73.0 55.8 17.2
28 20160629 65.8 73.2 NaN NaN
29 20160630 65.7 72.7 59.2 13.5
I'm assuming the error comes from the text in the data but I'm not sure how to make it ignore the text and just try to convert the measurements.
You don't need to loop. Pandas will automatically operate on all rows when you perform a calculation.
data['TEMP'] = round((data['TEMP'] - 32) / 1.8, 1)
Repeat this for all the columns containing temperatures. When the value is NaN
, it will automatically return NaN
as the result.