python - TypeError: unorderable types: str() > flo

2019-04-05 05:02发布

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')

Edit: 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)

1条回答
可以哭但决不认输i
2楼-- · 2019-04-05 05:26

Mask the nan values by using ~isnull():

mask = ~dataset['v3'].isnull()
dataset['v3'][mask] = enc.fit_transform(dataset['v3'][mask])

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]
查看更多
登录 后发表回答