我想采用温度为摄氏度的CSV文件并将其转换为华氏温度。
目前的尝试:
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)如果我只是在函数中输入一个整数,我能够成功地执行此操作,但是当我尝试使用csv文件时,我不断收到此错误消息:
can't multiply sequence by non-int of type 'float'我试图将值转换为浮动,但我没有运气。
编辑:我使用的CSV文件是多列。 它不仅仅具有空气温度。
这里还有完整的追溯
`--------------------------------------------------------------------------- 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')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')最满意答案
我之前没有使用过Pandas,但是查看了文档,看起来它应该可以工作:
df['Air Temperature'] = df['Air Temperature'].apply(f)'Air Temperature'是DataFrame中的一系列 ,而系列对象也有一个应用方法。
I've not used Pandas before but reviewing the documentation, this looks like it should work:
df['Air Temperature'] = df['Air Temperature'].apply(f)'Air Temperature' is a series in the DataFrame, and the series object also has an apply method.
更多推荐
发布评论