Reputation: 791
I have this dataframe:
Columns:
Countries
IsVertical
Example:
USA FALSE
USA FALSE
Poland FALSE
Italy TRUE
Italy TRUE
I want to create a bar chart that has 2 columns for each country (1 for True and one for false - From IsVertical) and I need the y axis to be the normalized count of times that the country appears.
I tryed something like this:
data.groupby("Country").count().plot(kind="bar")
The problem is that it doesn't give me 2 bars of IsVertical foreach country and that it only counts the number of times that IsVertical appears and doesn't divide by the total count of IsVertical
SOLUTION:
result = data.groupby('Country').apply(
lambda group: (group.IsVertical.sum() / float(data.IsVertical.sum()))
).to_frame('Vertical')
result.plot(kind='bar')
Upvotes: 0
Views: 1517
Reputation: 109546
result = df.groupby('Countries').apply(
lambda group: (group.IsVertical == 'TRUE').sum() /
float(group.IsVertical.count())
).to_frame('Vertical')
result['NotVertical'] = 1 - result.Vertical
>>> result
Vertical NotVertical
Countries
Italy 1 0
Poland 0 1
USA 0 1
result.plot(kind='bar')
Upvotes: 1