如何根据 value_counts 重新标记类别,然后绘制数据

How to relabel a category based on value_counts and then plot the data

我有一个包含大约 16000 个条目和 12 列的数据框。我(希望)已经删除了重复项和 Nan 值。 我想在带有 Pandas 的饼图中可视化列 'brand' 中出现的次数。但是每个出现次数少于 20 次的 Brand 应该归为一组并命名为 'Freie Tankstellen'.

我已经: df_stations['brand'].value_counts().to_frame()< 20

但我不知道如何进行,提前谢谢!

My Dataframe

elias_lay_u_schisslbauer_simon2021-06-12_11-57 - Jupyter 笔记本

,uuid,name,brand,street,house_number,post_code,city,latitude,longitude,first_active,openingtimes_json,state,istFrei
0,0e18d0d3-ed38-4e7f-a18e-507a78ad901d,OIL! Tankstelle München,OIL!,EVERSBUSCHSTRASSE,33,80999,MÜNCHEN,48.1807,11.4609,1970-01-01 01:00:00+01,"{""openingTimes"":[{""applicable_days"":192,""periods"":[{""startp"":""07:00"",""endp"":""20:00""}]},{""applicable_days"":63,""periods"":[{""startp"":""06:00"",""endp"":""22:00""}]}]}",Bayern,True
1,44e2bdb7-13e3-4156-8576-8326cdd20459,bft Tankstelle,BFT TANKSTELLE,SCHELLENGASSE ,53,36304,ALSFELD,50.7520089,9.2790394,1970-01-01 01:00:00+01,"{""openingTimes"":[{""applicable_days"":63,""periods"":[{""startp"":""06:00"",""endp"":""22:00""}]},{""applicable_days"":64,""periods"":[{""startp"":""07:00"",""endp"":""21:00""}]}]}",Hessen,True
2,ad812258-94e7-473d-aa80-d392f7532218,bft Bonn-Bad Godesberg,BFT,GODESBERGER ALLEE,55,53175,BONN,50.6951,7.14276,1970-01-01 01:00:00+01,"{""openingTimes"":[{""applicable_days"":31,""periods"":[{""startp"":""06:00"",""endp"":""22:00""}]},{""applicable_days"":32,""periods"":[{""startp"":""07:00"",""endp"":""22:00""}]},{""applicable_days"":64,""periods"":[{""startp"":""08:00"",""endp"":""22:00""}]}]}",Nordrhein-Westfalen,True
  1. 使用 df.brand.value_counts() 添加 'total_count' 列到 df 使用 .merge
  2. 使用布尔索引重命名 'brand' 'total_count' 小于 .lt、20.
  3. 获取 'brand' 的新 .value_counts,并使用 pandas.DataFrame.plotkind='barh' 绘制水平条。如果品牌不多,就用kind='bar',改成figsizekind='pie'可以用,但是,虽然我喜欢pi,而且是大饼,我不喜欢,还是推荐pie图表。
    • 使用饼图而不是条形图的主要目的是直观地表明一组值是分数或百分比加起来就是一个整体。此消息的代价相当大:比较饼图的值比使用条形图更困难,因为与比较两个条形图的高度相比,查看器更难比较两条弧线所夹的角度。 - Bergstrom, Carl T.; West, Jevin D.. Calling Bullshit(第 179 页)。兰登书屋出版集团。 Kindle 版。
  • 使用 pandas v1.2.4matplotlib v3.4.2
import pandas as pd
import numpy as np  # for sample data

# sample data
data = ['Aloha Petroleum', 'Alon', 'American Gas', 'Amoco', 'ARCO', 'Billups', 'BP', "Buc-ee's", "Casey's General Stores", 'CEFCO', 'CENEX', 'Chevron', 'Circle K', 'Citgo', 'Clark Brands', 'Conoco', 'Costco', 'Crown', 'Cumberland Farms', 'Delta Sonic - Buffalo New York']

# probabilities for each brand
prob = [0.099, 0.099, 0.099, 0.0501, 0.0501, 0.0501, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.0009, 0.0009, 0.0009]

# sample dataframe
np.random.seed(2)
df = pd.DataFrame({'brand': np.random.choice(data, size=(16000,), p=prob)})

# add a column to the dataframe called total_count
df = df.merge(df.brand.value_counts(), left_on='brand', right_index=True).rename({'brand_y': 'total_count'}, axis=1)

# any brand with a total_count less than 20 is renamed
df.loc[df.total_count.lt(20), 'brand'] = 'Freie Tankstellen'

# plot the new value count with the updated brand name
df.brand.value_counts().plot(kind='barh', figsize=(7, 10))

相比于kind='pie'

df.brand.value_counts().plot(kind='pie', figsize=(7, 10))