[英]pandas - plot bar chart for multiple categories
我有這個數據框:
列:
Countries
IsVertical
例:
USA FALSE
USA FALSE
Poland FALSE
Italy TRUE
Italy TRUE
我想創建一個條形圖,每個國家有2列(對於True,為1,對於Is,則為false-從IsVertical),我需要y軸作為該國家出現的標准化次數。
我嘗試過這樣的事情:
data.groupby("Country").count().plot(kind="bar")
問題是,它不會給我每個國家2個IsVertical柱,並且它僅計算IsVertical出現的次數,並且不除以IsVertical的總數
解:
result = data.groupby('Country').apply(
lambda group: (group.IsVertical.sum() / float(data.IsVertical.sum()))
).to_frame('Vertical')
result.plot(kind='bar')
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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.