'Celsius to Fahrenheit conversion in CSV file
I want to take a CSV file with Temperatures in celsius and convert it to fahrenheit.
Current attempt:
import pandas as pd
df = pd.read_csv('/temperature_data.csv', )
def f(x):
x = x * 1.8 + 32
return float(x)
df['AirTemperature'] = df.apply(f, axis=1)
I am able to successfully do this if I simply input an integer into the function, but I keep getting this error message when I try to use the csv file:
can't multiply sequence by non-int of type 'float'
I've tried to convert values to float but I've had no luck.
Edit: The CSV file that I am using is multi column. It has more than simply air temp in it.
Also here is full traceback
`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-a63269740c5c> in <module>()
----> 1 df['AirTemperature'] = df.apply(f, axis=1)
/Users/pvayn/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4040 if reduce is None:
4041 reduce = True
-> 4042 return self._apply_standard(f, axis, reduce=reduce)
4043 else:
4044 return self._apply_broadcast(f, axis)
/Users/pvayn/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in _apply_standard(self, func, axis, ignore_failures, reduce)
4136 try:
4137 for i, v in enumerate(series_gen):
-> 4138 results[i] = func(v)
4139 keys.append(v.name)
4140 except Exception as e:
<ipython-input-3-895f5da25595> in f(x)
1 def f(x):
----> 2 x = x*1.8 + 32
3 return float(x)
/Users/pvayn/anaconda/lib/python2.7/site-packages/pandas/core/ops.pyc in wrapper(left, right, name, na_op)
647 lvalues = lvalues.values
648
--> 649 return left._constructor(wrap_results(na_op(lvalues, rvalues)),
650 index=left.index, name=left.name,
651 dtype=dtype)
/Users/pvayn/anaconda/lib/python2.7/site-packages/pandas/core/ops.pyc in na_op(x, y)
588 result = np.empty(len(x), dtype=x.dtype)
589 mask = notnull(x)
--> 590 result[mask] = op(x[mask], y)
591 else:
592 raise TypeError("{typ} cannot perform the operation "
TypeError: ("can't multiply sequence by non-int of type 'float'", u'occurred at index 0')
Solution 1:[1]
You could use the following code:
df.assign(Fahrenheit = lambda x: (9/5)*x['Celsius']+32)
The formulas are : Fahrenheit to Celsius formula:
(°F – 32) x 5/9 = °C AND
Celsius to Fahrenheit formula:
(°C × 9/5) + 32 = °F
Solution 2:[2]
f = 12 if I want to convert f in Celsius to Fahrenheit I tell you the simple formula day = f days = day * 9/5 + 32 if I print the days result will be 53.6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Flair |
Solution 2 | Ubaid Khan |