本文介绍了python-TypeError:无法排序的类型:str()>漂浮()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个csv文件并具有v3列,但该列具有一些"nan"行.除了行,我该怎么办.
i have a csv file and has v3 column but that column has some 'nan' rows.How can i except the rows.
dataset = pd.read_csv('mypath')
enc = LabelEncoder()
enc.fit(dataset['v3'])
print('fitting')
dataset['v3'] = enc.transform(dataset['v3'])
print('transforming')
print(dataset['v3'])
print('end')
V3列具有A,C,B,A,C,D 、、、 A,S,就像这样,我想将其转换为(1,2,3,1,2,4 ,, ,1,7)
V3 columns has A,C,B,A,C,D,,,A,S, like that,and i want to convert it to (1,2,3,1,2,4,,,1,7)
推荐答案
使用〜isnull()屏蔽nan值:
Mask the nan values by using ~isnull():
mask = ~dataset['v3'].isnull()
dataset['v3'][mask] = enc.fit_transform(dataset['v3'][mask])
另一种方法是使用pandas.factorize函数,该函数自动处理nan(将其分配为-1):
Another way is to use the pandas.factorize function, which takes care of the nans automatically (assigns them -1):
dataset['v3'] = dataset['v3'].factorize()[0]
这篇关于python-TypeError:无法排序的类型:str()>漂浮()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!