私がした唯一のことは、文字列を「[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.
あまりエレガントではありませんが、これでうまくいくはずです。
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]
コメントを追加