Bob*_*Bob 4 python pandas scikit-learn
我正在尝试对数据帧的一列进行单热编码.
enc = OneHotEncoder()
minitable = enc.fit_transform(df["ids"])
Run Code Online (Sandbox Code Playgroud)
但我得到了
弃用警告:传递1d数组作为数据在0.17中被弃用,并且在0.19中将ValueError用于表示.
这有解决方法吗?
我想你可以用get_dummies:
df = pd.DataFrame({'ids':['a','b','c']})
print (df)
ids
0 a
1 b
2 c
print (df.ids.str.get_dummies())
a b c
0 1 0 0
1 0 1 0
2 0 0 1
Run Code Online (Sandbox Code Playgroud)
编辑:
如果输入与列lists,第一投地str,除去[]通过strip,并呼吁get_dummies:
df = pd.DataFrame({'ids':[[0,4,5],[4,7,8],[5,1,2]]})
print(df)
ids
0 [0, 4, 5]
1 [4, 7, 8]
2 [5, 1, 2]
print (df.ids.astype(str).str.strip('[]').str.get_dummies(', '))
0 1 2 4 5 7 8
0 1 0 0 1 1 0 0
1 0 0 0 1 0 1 1
2 0 1 1 0 1 0 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3136 次 |
| 最近记录: |