我正在阅读多个文件,并希望使用pd.read_table转换器转换温度列。我用来将Kelvin转换为Farenheit的函数单独工作,但在使用converter参数时不起作用。我得到了TypeError: unsupported operand type(s) for -: 'str' and 'float'
。
目前,我的DataFrame看起来像这样
temp
date
2006-01-01 294.6
2006-02-01 294.1...
df [' temp']。dtype是dtype('float64')
我的功能:
def k2f(df):
return (df-273.15)*1.8+32.00
#also tried lambda x : (x-273.15)*1.8+32.00
单独使用功能:
>>>k2f(df)
temp
date
2006-01-01 70.61
2006-02-01 69.71...
我如何阅读我的数据文件,df = ...:
df = pd.read_table('pathto.txt',skiprows=9,delimiter='\s+',
usecols=(0,4),index_col=(0),
names=('date','temp'),na_values=('na'),parse_dates=True,
converters={4:k2f}) #also tried 'temp' keyword to same result
使用df = ... above:
时出现完整错误消息Traceback (most recent call last):
File "mycode.py", line 24, in <module>
df = pd.read_table('pathto.txt',skiprows=9,delimiter='\s+',usecols=(0,4),index_col=(0),names=('date','temp'),na_values=('na'),parse_dates=True,converters={4:k2f})
File "/usr/local/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 655, in parser_f
return _read(filepath_or_buffer, kwds)
File "/usr/local/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 411, in _read
data = parser.read(nrows)
File "/usr/local/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 982, in read
ret = self._engine.read(nrows)
File "/usr/local/anaconda2/lib/python2.7/site-packages/pandas/io/parsers.py", line 1719, in read
data = self._reader.read(nrows)
File "pandas/_libs/parsers.pyx", line 890, in pandas._libs.parsers.TextReader.read (pandas/_libs/parsers.c:10862)
File "pandas/_libs/parsers.pyx", line 912, in pandas._libs.parsers.TextReader._read_low_memory (pandas/_libs/parsers.c:11138)
File "pandas/_libs/parsers.pyx", line 989, in pandas._libs.parsers.TextReader._read_rows (pandas/_libs/parsers.c:12175)
File "pandas/_libs/parsers.pyx", line 1112, in pandas._libs.parsers.TextReader._convert_column_data (pandas/_libs/parsers.c:14103)
File "pandas/_libs/parsers.pyx", line 2274, in pandas._libs.parsers._apply_converter (pandas/_libs/parsers.c:30500)
File "mmm_a.py", line 20, in k2f
return (df-273.15)*1.8+32.00
TypeError: unsupported operand type(s) for -: 'str' and 'float'
一如既往,任何帮助将不胜感激。这个社区很棒!
干杯
***使用键的引号,&#39; 4&#39;似乎可以消除错误。 DF = ...
df = pd.read_table('pathto.txt',skiprows=9,delimiter='\s+',
usecols=(0,4),index_col=(0),
names=('date','temp'),na_values=('na'),parse_dates=True,
***converters={'4':k2f}) #though 'temp' still doesn't work, same error
但是,该功能未应用于这些值。 I.E. df打印,但值没有改变。
答案 0 :(得分:0)
如果我理解文档正确,可能转换器应该期望一个字符串而不是浮点数:
dtype:列名称或列表 - &gt;类型,默认无
数据或列的数据类型。例如。 {'a':np.float64,'b':np.int32} 使用str或object来保留和不解释dtype。如果转换器 如果指定,它们将应用于dtype转换的INSTEAD。
所以可能发生的事情是该列未转换为float,而是将转换器直接应用于正在读取的字符串。