Python 熊猫:如何绘制imdb电影总预算与熊猫的单独类型?

Python 熊猫:如何绘制imdb电影总预算与熊猫的单独类型?,python,pandas,Python,Pandas,这实际上是我上一个问题的后续问题 在这个问题上,我们为电影绘制了uniques类型的数量。 我的问题是:如何获得'budget'与'genres'在pandas中的绘图 以下是示例代码: import pandas as pd import numpy as np %matplotlib inline df = pd.DataFrame({'movie' : ['A', 'B','C','D'], 'budget': [1000, 2000, 3000

这实际上是我上一个问题的后续问题

在这个问题上,我们为电影绘制了uniques类型的数量。 我的问题是:如何获得
'budget'
'genres'
pandas
中的绘图

以下是示例代码:

import pandas as pd
import numpy as np 
%matplotlib inline

df = pd.DataFrame({'movie' : ['A', 'B','C','D'],
                   'budget': [1000, 2000, 3000, 4000],
                   'genres': ['Science Fiction|Romance|Family', 'Action|Romance',
                              'Family|Drama','Mystery|Science Fiction|Drama']},
                  index=range(4))
df
这里的体裁
科幻小说|浪漫|家庭
实际上是三种不同的体裁


科幻小说
以moives
A
B
的形式出现,因此
科幻小说
的预算应该是
1000+4000=5000
,依此类推。

以下是您可以为每种类型绘制总预算的方法:

genres = (df.genres.str.split('|', expand=True)
            .stack()
            .to_frame(name='genre'))


genres.index = genres.index.droplevel(1)
因此,
genres
成为:

        genre
0   Science Fiction
0   Romance
0   Family
1   Action
1   Romance
2   Family
2   Drama
3   Mystery
3   Science Fiction
3   Drama
现在执行加入和分组,首先获取预算信息,然后根据类型求和:

(genres.join(df['budget'])
       .groupby('genre')
       .sum()
       .plot(kind='bar'))
输出:


以下是如何绘制每种类型的总预算:

genres = (df.genres.str.split('|', expand=True)
            .stack()
            .to_frame(name='genre'))


genres.index = genres.index.droplevel(1)
因此,
genres
成为:

        genre
0   Science Fiction
0   Romance
0   Family
1   Action
1   Romance
2   Family
2   Drama
3   Mystery
3   Science Fiction
3   Drama
现在执行加入和分组,首先获取预算信息,然后根据类型求和:

(genres.join(df['budget'])
       .groupby('genre')
       .sum()
       .plot(kind='bar'))
输出:


使用
df.plot(x='genres',y='budget')
进行简单的线图绘制。你应该试着在第一步就把你的问题和你想要的东西说得更清楚。这节省了每个人进行编辑的时间。使用
df.plot(x='genres',y='budget')
进行简单的线图。你应该试着在第一步就把你的问题和你想要的东西说得更清楚。这节省了每个人进行编辑的时间。