Python字符串比较错误
将二进制d.type_str变量转换为'bid'或'ask'时,我遇到以下错误。谢谢你们的帮助!我正在使用python 2.7 我的代码:Python字符串比较错误,python,string,types,pandas,Python,String,Types,Pandas,将二进制d.type_str变量转换为'bid'或'ask'时,我遇到以下错误。谢谢你们的帮助!我正在使用python 2.7 我的代码: from itertools import izip_longest import itertools import pandas import numpy as np all_trades = pandas.read_csv('C:\\Users\\XXXXX\\april_trades.csv', parse_dates=[0], index_col
from itertools import izip_longest
import itertools
import pandas
import numpy as np
all_trades = pandas.read_csv('C:\\Users\\XXXXX\\april_trades.csv', parse_dates=[0], index_col=0)
usd_trades = all_trades[all_trades['d.currency'] == 'USD']
volume = (usd_trades['d.amount_int'])
trades = (usd_trades['d.price_int'])
def cleanup(x):
if isinstance(x, str) and 'e-' in x:
return 0
else:
return float(x)
volume = volume.apply(lambda x: cleanup(x))
volume = volume.astype(float32)
#####
typestr = (usd_trades['d.type_str'])
typestr[typestr == 'bid'] = 0
typestr[typestr == 'ask'] = 1
错误输出:
>>> typestr[typestr == 'ask'] = 1
File "C:\Anaconda\lib\site-packages\pandas\core\series.py", line 240, in wrapper
% type(other))
TypeError: Could not compare <type 'str'> type with Series
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>typestr[typestr=='ask']=1
包装器中的文件“C:\Anaconda\lib\site packages\pandas\core\series.py”,第240行
%类型(其他))
TypeError:无法将类型与序列进行比较
>>>回溯(最近一次呼叫最后一次):
文件“”,第1行,在
如您所述,您的类型str
是二进制的。当您尝试将字符串与序列与int
数据进行比较时,Pandas会抱怨,请参阅
>>> s = pd.Series([1], dtype=np.int64)
>>> s == 'a'
Traceback (most recent call last):
...
TypeError: Could not compare <type 'str'> type with Series
是不是
系列
熊猫。系列
?是的,我读了熊猫的csv,数据框中系列的列标题是d.typ_strI,我在+1之前添加了前几行,不是为了一个冷静的答案,而是为了能够从问题和错误中猜出原始意图。顺便说一句,如果这是int-to-string,那么美元交易['d.type\u str']=np.选择(美元交易['d.type\u str'],['bid','ask'])怎么样
>>> typestr[typestr == 1] = 'ask'