如何对pandas数据帧的一列进行onehot编码?

2024-10-01 15:31:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试对一个数据帧的一列进行热编码。在

enc = OneHotEncoder()
minitable = enc.fit_transform(df["ids"])

但我得到了

DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.

有解决办法吗?在


Tags: 数据inids编码dfdataastransform
1条回答
网友
1楼 · 发布于 2024-10-01 15:31:51

我想你可以用^{}

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

编辑:

如果输入是带有lists的列,则首先转换为str,通过^{}移除{},然后调用{a1}:

^{pr2}$

相关问题 更多 >

    热门问题