pandas 数据
Published on Aug. 22, 2023, 12:11 p.m.
pandas 数据快速 处理成标签数据
from sklearn import preprocessing
le = preprocessing.LabelEncoder()
le.fit(df.fruit)
df['categorical_label'] = le.transform(df.fruit)
#解码回到原始数据.
le.inverse_transform(df['categorical_label'])
You can use sklearn.preprocessing
具体参考网址
https://stackoverflow.com/questions/42320834/sklearn-changing-string-class-label-to-int
pandas 快速处理onehot
import pandas as pd
df = pd.DataFrame({
'A':['a','b','a'],
'B':['b','a','c']
})
df
Out[]:
A B
0 a b
1 b a
2 a c
# Get one hot encoding of columns B
one_hot = pd.get_dummies(df['B'])
# Drop column B as it is now encoded
df = df.drop('B',axis = 1)
# Join the encoded df
df = df.join(one_hot)
df
Out[]:
A a b c
0 a 0 1 0
1 b 1 0 0
2 a 0 0 1
参考网址
https://stackoverflow.com/questions/37292872/how-can-i-one-hot-encode-in-python
数据转化成list
df.values.tolist()