Reputation:
I know how to plot something like this:
SEASON1 SEASON2 SEASON3
area
A 299.0 2.0 257.0
B 13.0 33.0 198.0
C 22044.0 2.0 22.0
Using
df.plot(kind='bar', stacked=True, rot=90, edgecolor='black')
df.T.plot(kind='bar', stacked=True, rot=0, edgecolor='black')
Resulting in:
I'm having hard time to obtain the same (or even better looking) plots for the following df
which is representing the original df
but made more elegantly here.
Upvotes: 1
Views: 161
Reputation: 3306
What you want to do is to unstack your dataframe, and change the name of the columns.
You can do so by doing :
df.unstack()
.rename(columns = {
"2016Q1" : "Season 1",
"2016Q2" : "Season 2",
"2016Q3" : "Season 3",
})
You can find examples on the documentation about what unstack does, and how it is doing it. As for the rename method, it takes a mapping to convert your names from something to something else.
I didn't try to make your example work, but I took an example from the unstack documentation above.
index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
('two', 'a'), ('two', 'b')])
df = pd.DataFrame( np.arange(1.0, 5.0), index=index, columns=['hi'])
print(df)
# hi
# one a 1.0
# b 2.0
# two a 3.0
# b 4.0
df = df.unstack(level = -1)
.rename(columns = {
"a" : "Season 1",
"b" : "Season 2"
})
print(df)
# hi
# Season 1 Season 2
# one 1.0 2.0
# two 3.0 4.0
There could be a better way to handle the "hi" above your DataFrame but you can just select
it, and it'll disappear.
print( s['hi'] )
Season 1 Season 2
one 1.0 2.0
two 3.0 4.0
Upvotes: 1