I have a problem about showing bar graph in pandas. Some rating values cannot be defined because of the result of query shown below. But I want to show all both missing values and defined values in bar plot graph. How can I fix it?
这是我的代码段和结果
netflix_df.query("type == 'TV Show'")["rating"].value_counts()
TV-14 39
TV-MA 32
TV-PG 14
TV-Y7 6
TV-Y 3
R 1
TV-G 1
从查询中获得结果后,我想以条形图显示,但是这里有错误。
rating_order = ['G', 'TV-Y', 'TV-G', 'PG', 'TV-Y7', 'TV-Y7-FV', 'TV-PG', 'PG-13', 'TV-14', 'R', 'NC-17', 'TV-MA']
tv_show_rating = netflix_df.query("type == 'TV Show'")["rating"].value_counts()[rating_order].fillna(0) <---- HERE IS AN ERROR
rating_barplot(tv_show_rating,'TV Show', 40,'images/image6.png')
我修改了下面显示的代码后。
rating_order = ['G', 'TV-Y', 'TV-G', 'PG', 'TV-Y7', 'TV-Y7-FV', 'TV-PG', 'PG-13', 'TV-14', 'R', 'NC-17', 'TV-MA']
tv_show_rating = netflix_df.query("type == 'TV Show'")["rating"].value_counts().reindex([rating_order])
错误定义如下。
TypeError: ('G',) is not a string
答案 0 :(得分:0)
您的问题不清楚。假设您要绘制查询的条形图。您可以将查询结果放入新的数据框中。在这种情况下,我将其放在df
中。参见下面的df
df
尝试按以下方式绘制
import matplotlib.pyplot as plt
fig, ax=plt.subplots()
ax.bar(df.Rating,df.Count)
plt.show()
答案 1 :(得分:0)
这是我的解决方法
rating_order = ['G', 'TV-Y', 'TV-G', 'PG', 'TV-Y7', 'TV-Y7-FV', 'TV-PG', 'PG-13', 'TV-14', 'R', 'NC-17', 'TV-MA']
tv_show_rating = netflix_df.query("type == 'TV Show'")["rating"].value_counts().reindex(rating_order).fillna(0)