0
我想以攝氏溫度拍攝一個CSV文件並將其轉換爲華氏度。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')
您給出的代碼適用於我,包含浮點數和整數的單列CSV。我們能看到完整的追溯? –
似乎很清楚,該函數收到的'x'是一個序列類型,而不是一個int。這似乎在文檔中提到:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html您可能需要修改'f'來適應這一點。 –
你有沒有建議我如何接近@DavidZemens? – pvayn