简体   繁体   English

如何从此数据框创建条形图?

[英]How can I create a bar graph from this Dataframe?

I have this dataframe ( https://i.stack.imgur.com/hRD1H.jpg ) that I created from an SQL query.我有从 SQL 查询创建的这个数据框 ( https://i.stack.imgur.com/hRD1H.jpg )。 I want to create a bar graph that shows the frequency for each type of movie genre, so I can see what the top genre is.我想创建一个条形图来显示每种电影类型的频率,这样我就可以看到最热门的类型是什么。

My problem is that in the genre column, each value is compromised of multiple genres.我的问题是,在流派列中,每个值都包含多种流派。 But I want to separate each into its own genre.但我想把每一个分成自己的流派。 So say I have a movie whose genre is "Action, Thriller".假设我有一部电影的类型是“动作,惊悚”。 I want to be able to count those as two separate entries.我希望能够将它们算作两个单独的条目。

I have been trying to work on this for days, but for the life of me I cannot figure out syntax to be able to do this.几天来我一直在努力解决这个问题,但在我的一生中,我无法弄清楚能够做到这一点的语法。 Should I do the actual separating in my SQL query, or should I do it when working with the DF?我应该在我的 SQL 查询中进行实际的分离,还是应该在使用 DF 时这样做? Any help would be greatly appreciated.任何帮助将不胜感激。

I haven't seen SQL in a long time, so I can't say about it.好久没看到SQL了,不能多说。

But in python, I would do something like this:但是在python中,我会做这样的事情:

def count_genre(genre_array):

    genre_array_sep = []
    counts = []
    for g in genre_array:
        genre_array_sep.append(g.split(", "))
    # print(genre_array_sep)

    options = ["Thriller", "Drama", "Action"]
    for op in options:
        count = 0
        for g in genre_array_sep:
            if op in g:
                count += 1
                g.remove(op)
        # print(genre_array_sep)
        counts.append(count)

    return counts

# input
film_genre = ["Thriller", "Drama", "Action, Thriller", "Action", "Action"]
# output
print(count_genre(film_genre))

But please bear in mind that I'm not a programmer so there is certainly a better/faster solution.但请记住,我不是程序员,所以肯定有更好/更快的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM