获得无效的类型比较错误

时间:2018-03-15 05:25:35

标签: python python-3.x data-mining

如果有人可以提供帮助,我会收到无效的类型比较错误?基本上,我在数据框中用零替换所有“ - ”的行上出现错误,以便使数字操作均匀。以下是我的代码和错误:

代码:

import quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

import time
import seaborn as sns
import matplotlib.pyplot as plt
import datetime
from matplotlib import style
style.use('ggplot')

# get market info for bitcoin from the start of 2016 to the current day
bitcoin_market_info = pd.read_html("https://coinmarketcap.com/currencies/ethereum/historical-data/?start=20130428&end="+time.strftime("%Y%m%d"))[0]
# convert the date string to the correct date format
bitcoin_market_info = bitcoin_market_info.assign(Date=pd.to_datetime(bitcoin_market_info['Date']))
# when Volume is equal to '-' convert it to 0
#In the line below I am getting error
bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']=0
# convert to int
bitcoin_market_info['Volume'] = bitcoin_market_info['Volume'].astype('int64')

bitcoin_market_info.set_index('Date', inplace=True)
bitcoin_market_info = bitcoin_market_info.reindex(index=bitcoin_market_info.index[::-1])
# look at the first few rows
bitcoin_market_info.head()

错误:

F:\Anaconda3\lib\site-packages\pandas\core\ops.py:798: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
result = getattr(x, name)(y)
Traceback (most recent call last):
File "C:/Users/The_Anil/Desktop/fvsffsf.py", line 20, in <module>
bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']=0
File "F:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 861, in wrapper
res = na_op(values, other)
File "F:\Anaconda3\lib\site-packages\pandas\core\ops.py", line 800, in na_op
raise TypeError("invalid type comparison")
TypeError: invalid type comparison

1 个答案:

答案 0 :(得分:0)

问题在这里

bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']=0

您正在将其与字符串进行比较

bitcoin_market_info['Volume']=="-"

好像变量是字符串类型,然后尝试将整数类型值分配给它的末尾的同一个变量。

我猜你的变量音量是字符串类型,所以你可以这样做

bitcoin_market_info.loc[bitcoin_market_info['Volume']=="-",'Volume']="0"

然后您可以将变量转换为整数类型