'description'列のDataFrameがあり、説明内の単語の単語数を含む1つのホットエンコーディングを作成したいと思います。
description
0 test words that describe things
1 more and more words here
2 things test
必要な出力
test words that describe things more here and
0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0
1 0.0 1.0 0.0 0.0 0.0 2.0 1.0 1.0
2 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0
私が持っている現在の解決策は次のとおりです。
one_hot = df.apply(lambda x: pd.Series(x.description).str.split(expand=True).stack().value_counts(), axis=1)
これは、大きなデータセット(130K行)では非常に遅くなり(1行あたり2.6ミリ秒)、より良い解決策があるかどうか疑問に思いました。また、1つのエントリにのみ表示される単語を削除したいと思います。
test words things
0 1.0 1.0 1.0
1 0.0 1.0 0.0
2 1.0 0.0 1.0
IIUCは、カウントのために、あなたが行うことができますgroupby+sum
上のaxis=1
後get_dummies
final = (pd.get_dummies(df['description'].str.split(expand=True))
.groupby(lambda x: x.split('_')[-1],axis=1).sum())
またはapply(slower)を使用:
df['description'].str.split(expand=True).apply(pd.value_counts,axis=1).fillna(0)
and describe here more test that things words
0 0 1 0 0 1 1 1 1
1 1 0 1 2 0 0 0 1
2 0 0 0 0 1 0 1 0
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加