Create a Frequency Table in Python

Your goal

You have a Pandas DataFrame containing a categorical variable, and you want to create a frequency table.

Step-by-step tutorial

Use DataFrame.value_counts() to create the frequency table:

>>> import pandas as pd
>>> crime_df = pd.read_csv('crime_sf_2016.csv')
>>> crime_df
        IncidntNum        Category                                           Descript DayOfWeek                    Date  ...                   Address           X          Y                               Location            PdId
0        120058272     WEAPON LAWS                          POSS OF PROHIBITED WEAPON    Friday  01/29/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  12005827212120
1        120058272     WEAPON LAWS     FIREARM, LOADED, IN VEHICLE, POSSESSION OR USE    Friday  01/29/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  12005827212168
2        141059263        WARRANTS                                     WARRANT ARREST    Monday  04/25/2016 12:00:00 AM  ...     KEITH ST / SHAFTER AV -122.388856  37.729981  (37.7299809672996, -122.388856204292)  14105926363010
3        160013662    NON-CRIMINAL                                      LOST PROPERTY   Tuesday  01/05/2016 12:00:00 AM  ...    JONES ST / OFARRELL ST -122.412971  37.785788  (37.7857883766888, -122.412970537591)  16001366271000
4        160002740    NON-CRIMINAL                                      LOST PROPERTY    Friday  01/01/2016 12:00:00 AM  ...      16TH ST / MISSION ST -122.419672  37.765050  (37.7650501214668, -122.419671780296)  16000274071000
...            ...             ...                                                ...       ...                     ...  ...                       ...         ...        ...                                    ...             ...
150495   161061000         ASSAULT                                            BATTERY    Friday  12/30/2016 12:00:00 AM  ...       OAK ST / STANYAN ST -122.453982  37.771428  (37.7714278595913, -122.453981622365)  16106100004134
150496   176000742    NON-CRIMINAL                                      LOST PROPERTY    Friday  12/30/2016 12:00:00 AM  ...   JACKSON ST / SANSOME ST -122.401857  37.796626  (37.7966261239618, -122.401857374739)  17600074271000
150497   176000758   LARCENY/THEFT                            PETTY THEFT OF PROPERTY  Thursday  12/29/2016 12:00:00 AM  ...       PINE ST / TAYLOR ST -122.412269  37.790673   (37.7906727649886, -122.41226909106)  17600075806372
150498   176000764   LARCENY/THEFT                            GRAND THEFT OF PROPERTY    Friday  12/30/2016 12:00:00 AM  ...  200 Block of STOCKTON ST -122.406659  37.788275  (37.7882745285785, -122.406658711008)  17600076406374
150499   179002868  OTHER OFFENSES  FRAUDULENT GAME OR TRICK, OBTAINING MONEY OR P...    Friday  12/02/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  17900286809024

[150500 rows x 13 columns]
>>> category_counts = crime_df['Category'].value_counts()
>>> category_counts
LARCENY/THEFT                  40409
OTHER OFFENSES                 19599
NON-CRIMINAL                   17866
ASSAULT                        13577
VANDALISM                       8589
VEHICLE THEFT                   6419
WARRANTS                        5914
BURGLARY                        5802
SUSPICIOUS OCC                  5782
MISSING PERSON                  4338
DRUG/NARCOTIC                   4243
ROBBERY                         3299
FRAUD                           2635
SECONDARY CODES                 1841
TRESPASS                        1812
WEAPON LAWS                     1658
SEX OFFENSES, FORCIBLE           940
STOLEN PROPERTY                  882
RECOVERED VEHICLE                736
DISORDERLY CONDUCT               658
PROSTITUTION                     641
FORGERY/COUNTERFEITING           619
DRUNKENNESS                      465
DRIVING UNDER THE INFLUENCE      378
ARSON                            286
KIDNAPPING                       257
EMBEZZLEMENT                     168
LIQUOR LAWS                      156
RUNAWAY                          140
SUICIDE                           69
BRIBERY                           66
EXTORTION                         60
FAMILY OFFENSES                   53
LOITERING                         42
SEX OFFENSES, NON FORCIBLE        40
BAD CHECKS                        34
GAMBLING                          20
PORNOGRAPHY/OBSCENE MAT            4
TREA                               3
Name: Category, dtype: int64