[英]How to plot a pie chart with four column in the dataset by using Plotly in python
[英]Python how to plot a frequency pie chart with one column using plotly.express
我有一個數據框,我只想 plot 一列的頻率,例如不同品牌的汽車數量。 汽車:[福特,福特,寶馬,GMC,GMC,GMC,GMC .....] 我想在任何合適圖形的餅圖中 plot,而不使用 matplotlib。
我嘗試創建 pivot 表,類似於福特:4,雪佛蘭:3,寶馬:5,GMC:10。但我不知道如何訪問列標簽,也無法在 plotly 中使用它們。
您可以按cars
分組並在新列count
中獲取計數,如下所示
df.groupby(['cars'])['cars'].count().reset_index(name='count')
然后您可以使用Plotly像這樣渲染餅圖
import plotly.express as px fig = px.pie(df, values='count', names='cars', title='Cars') fig.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.