简体   繁体   中英

Matplotlib: how to create stacked bar plot from pandas data frame?

Starting from the following

df = pd.DataFrame( {'Item':['A','A','A','B','B','C','C','C','C'], 
    'Name': ['Tom','John','Paul','Tom','Frank','Tom', 'John', 'Richard', 'James'],
    'Total':[3,3,3,2,2,4,4,4,4]})
print df
  Item     Name
0    A      Tom
1    A     John
2    A     Paul
3    B      Tom
4    B    Frank
5    C      Tom
6    C     John
7    C  Richard
8    C    James

#merge M:N by column Item
df1 = pd.merge(df, df, on=['Item'])

#remove duplicity - column Name_x == Name_y
df1 = df1[~(df1['Name_x'] == df1['Name_y'])]
#print df1

#create lists
df1 = df1.groupby('Name_x')['Name_y'].apply(lambda x: x.tolist()).reset_index()
print df1
    Name_x                                     Name_y
0    Frank                                      [Tom]
1    James                       [Tom, John, Richard]
2     John           [Tom, Paul, Tom, Richard, James]
3     Paul                                [Tom, John]
4  Richard                         [Tom, John, James]
5      Tom  [John, Paul, Frank, John, Richard, James]

I have a dataframe as the following:

print df 
      Name                               People            times
0    Frank                                [Tom]              [1]
1    James                 [John, Richard, Tom]        [1, 1, 1]
2     John          [James, Paul, Richard, Tom]     [1, 1, 1, 2]
3     Paul                          [John, Tom]           [1, 1]
4  Richard                   [James, John, Tom]        [1, 1, 1]
5      Tom  [Frank, James, John, Paul, Richard]  [1, 1, 2, 1, 1]

I want to create a stacked bar plot for each Name considering People as bar and times as values.

I want to do something like this

sub_df = df.groupby(['Name','People'])['Times'].sum().unstack()
sub_df.plot(kind='bar',stacked=True)

but it returns

TypeError: unhashable type: 'numpy.ndarray'

You have to use apply for the flexible type of 'agg' after groupby :

df1['People'] = df1['Name_y'].apply(lambda x: tuple(x))
df1['Times'] = df1['Name_y'].apply(lambda x: [x.count(name) for name in list(set(x))])
s = df1.groupby(['Name_x','People']).apply(lambda x: sum(x.iloc[0]['Times']))

Then you get the following

Name_x   People                                   
Frank    (Tom,)                                       1
James    (Tom, John, Richard)                         3
John     (Tom, Paul, Tom, Richard, James)             5
Paul     (Tom, John)                                  2
Richard  (Tom, John, James)                           3
Tom      (John, Paul, Frank, John, Richard, James)    6
dtype: int64

And you can plot as you like

s.plot(kind='bar', stacked=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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