簡體   English   中英

數據框水平堆積條形圖

[英]Dataframe horizontal stacked bar plot

我正在閱讀 movielens 用戶數據。 我想繪制按性別分組的年齡和職業(在兩個單獨的圖中)。 但我收到此錯誤:

user_df.groupby(['gender'])['age'].unstack().plot.bar()

AttributeError: 無法訪問“SeriesGroupBy”對象的可調用屬性“unstack”,請嘗試使用“apply”方法

我希望該圖類似於http://benalexkeen.com/bar-charts-in-matplotlib/ 中的示例數據格式如下:

user_id age gender  occupation  zipcode
0   1   24  M   technician  85711
1   2   53  F   other   94043
2   3   23  M   writer  32067
3   4   24  M   technician  43537
4   5   33  F   other   15213

你可以嘗試這樣的事情:

df.groupby(['occupation'])['user_id'].nunique().plot.bar()

對於性別職業,您可以執行以下操作:

df.groupby(['occupation','gender'])['user_id'].size().unstack().plot.bar()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM