Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a Dataframe with around 16000 entries and 12 columns. I've (hopefully) already removed duplicates and Nan values. I want to visualise the number of occurrences in the column 'brand' in a Pie chart with Pandas. But every Brand which occurs less than 20 times should be grouped together and be named 'Freie Tankstellen'.

I've gotten to: df_stations['brand'].value_counts().to_frame()< 20

But i don't know how to proceed, thank you in advance!

My Dataframe

elias_lay_u_schisslbauer_simon2021-06-12_11-57 - Jupyter Notebook

,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
like image 475
Elias Lay Avatar asked Nov 24 '25 11:11

Elias Lay


1 Answers

  1. Use df.brand.value_counts() to add a 'total_count' column to the df using .merge.
  2. Use Boolean indexing to rename any 'brand' with a 'total_count' less than, .lt, 20.
  3. Get the new .value_counts for 'brand', and plot a horizontal bar using pandas.DataFrame.plot with kind='barh'. If there aren't many brands, use kind='bar' and change figsize. kind='pie' can be used, but, while I like pi, and pieces of pie, I do not like, or recommend pie charts.
    • The main purpose of using a pie chart, rather than a bar graph, is to visually indicate that a set of values are fractions or percentages that add up to a whole. This message comes at a considerable cost: Comparing values is more difficult with a pie chart than with a bar chart because is harder for the viewer to compare the angles subtended by two arcs than to compare the height for two bars. - Bergstrom, Carl T.; West, Jevin D.. Calling Bullshit (p. 179). Random House Publishing Group. Kindle Edition.
  • Using pandas v1.2.4 and matplotlib 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))

enter image description here

Compared to kind='pie'

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

enter image description here

like image 140
Trenton McKinney Avatar answered Nov 25 '25 23:11

Trenton McKinney