Python Pandas:TypeError:float() 参数必须是字符串或数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41256626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas : TypeError: float() argument must be a string or a number
提问by Gingerbread
I have a dataframe that contains
我有一个包含
user_id date browser conversion test sex age country
1 2015-12-03 IE 1 0 M 32.0 US
This is my entire code thus far!
到目前为止,这是我的全部代码!
data["country"].fillna("missing")
data["age"].fillna(-10000, inplace=True)
data["ads_channel"].fillna("missing")
data["sex"].fillna("missing")
data['date'] = pd.to_datetime(data.date)
columns = data.columns.tolist()
columns = [c for c in columns if c not in ["test"]]
from sklearn import tree
clf = tree.DecisionTreeClassifier(max_depth=2, min_samples_leaf = (len(data)/100) )
clf = clf.fit(data[columns],data["test"])
I am getting this error:
我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-560-95a8a54aa939> in <module>()
4 from sklearn import tree
5 clf = tree.DecisionTreeClassifier(max_depth=2, min_samples_leaf = (len(data)/100) )
----> 6 clf = clf.fit(data[columns],data["test"])
C:\Users\SnehaPriya\Anaconda2\lib\site-packages\sklearn\tree\tree.pyc in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
152 random_state = check_random_state(self.random_state)
153 if check_input:
--> 154 X = check_array(X, dtype=DTYPE, accept_sparse="csc")
155 if issparse(X):
156 X.sort_indices()
C:\Users\SnehaPriya\Anaconda2\lib\site-packages\sklearn\utils\validation.pyc in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
371 force_all_finite)
372 else:
--> 373 array = np.array(array, dtype=dtype, order=order, copy=copy)
374
375 if ensure_2d:
TypeError: float() argument must be a string or a number
I am still learning to code and I would like to know how to overcome this error. Any help will be much appreciated!
我仍在学习编码,我想知道如何克服这个错误。任何帮助都感激不尽!
采纳答案by jezrael
IIUC you need exclude column date
also:
IIUC 您还需要排除列date
:
columns = [c for c in columns if c not in ["test", 'date']]
because error:
因为错误:
TypeError: float() argument must be a string or a number, not 'Timestamp'
类型错误:float() 参数必须是字符串或数字,而不是“时间戳”