列の行を分割し、出現する各単語の数を見つけ、棒グラフを使用して最も多い単語を見つけます

KnowTooLess
  • 私はデータフレームを持っています
  • 文字列を各行に分割したいのですが、
  • 表示される各単語の数を見つけて、すべての単語を数えます
  • 最高のものを視覚化するための棒グラフを作成します。

私がした唯一のことは、文字列を「[x | x | x]」から「[x、x、x]」に分割することですが、各単語を数える方法は混乱しています。

df_genres = df.copy()
df_genres.genres.head()

    0    Action|Adventure|Science Fiction|Thriller
    1    Action|Adventure|Science Fiction|Thriller
    2           Adventure|Science Fiction|Thriller
    3     Action|Adventure|Science Fiction|Fantasy
    4                        Action|Crime|Thriller
    Name: genres, dtype: object
-----------------------------------------------------------------
s = df_genres['genres'].str.split('|')
s.head()

    0    [Action, Adventure, Science Fiction, Thriller]
    1    [Action, Adventure, Science Fiction, Thriller]
    2            [Adventure, Science Fiction, Thriller]
    3     [Action, Adventure, Science Fiction, Fantasy]
    4                         [Action, Crime, Thriller]
    Name: genres, dtype: object

- count each word like"Action, Adventure, Fiction, etc",
- make the bar chart and see which word has the highest bar.
user7440787

あまりエレガントではありませんが、これでうまくいくはずです。

import pandas as pd
import matplotlib.pyplot as plt

df_genres = pd.DataFrame({'genres': ["Action|Adventure|Science Fiction|Thriller", "Action|Adventure|Science Fiction|Thriller", "Adventure|Science Fiction|Thriller", "Action|Adventure|Science Fiction|Fantasy", "Action|Crime|Thriller" ] })

genres_count = {j : ''.join(df_genres.genres.tolist()).count(j) for i in df_genres.genres.str.split('|').tolist() for j in i}

pd.DataFrame({'genres': list(genres_count.keys()), 'count': list(genres_count.values())}).plot.bar(x='genres', y='count')

plt.show()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

TOP 一覧

  1. 1

    三項演算子良い練習の代わりとしてOptional.ofNullableを使用していますか?

  2. 2

    STSでループプロセス「クラスパス通知の送信」のループを停止する方法

  3. 3

    Spring Boot Filter is not getting invoked if remove @component in fitler class

  4. 4

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  5. 5

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  6. 6

    Ansibleで複数行のシェルスクリプトを実行する方法

  7. 7

    Python / SciPyのピーク検出アルゴリズム

  8. 8

    画像変更コードを実行してもボタンの画像が変更されない

  9. 9

    ビュー用にサイズ変更した後の画像の高さと幅を取得する方法

  10. 10

    VisualStudioコードの特異点/ドッカー画像でPythonインタープリターを使用するにはどうすればよいですか?

  11. 11

    Three.js indexed BufferGeometry vs. InstancedBufferGeometry

  12. 12

    __init__。pyファイルの整理中に循環インポートエラーが発生しました

  13. 13

    二次導関数を数値計算するときの大きな誤差

  14. 14

    値間の一致を見つける最も簡単な方法は何ですか

  15. 15

    androidsoongビルドシステムによるネイティブコードカバレッジ

  16. 16

    Reactでclsxを使用する方法

  17. 17

    How to access json value by key value in freemarker?

  18. 18

    エンティティIDを含む@RequestBody属性をSpringの対応するエンティティに変換します

  19. 19

    PyTesseractを使用した背景色のため、スクリーンショットからテキストを読み取ることができません

  20. 20

    Using Angular's UI-router, how can we make sure the new version of the html partial views are used, rather than the cached version?

  21. 21

    symfonyエラーサーバーが404NotFoundを返しました

ホットタグ

アーカイブ