Q:

How to find if a frequency table key column contains all values from a list

I have a data frame, and I am doing a frequency table count for a column. I want to search if the key column in the table contains all values in this list ls = ['Agree', 'Disagree', 'Neutral', 'N/A'], if the column key misses some values in the list, I want to add the missing values to the column and make their count as 0. Is that something that can be executed when calculating the frequency table?
For example, I have already calculated my frequency table using value_counts here and transform it into data frame.
# df
key        count
Agree       22
Disagree    30
Neutral     12

Expected output
key        count
Agree       22
Disagree    30
Neutral     12
N/A          0

Thank you.

A:

Convert first values of key column to Categoricals, so if missing key by default are added new row with 0 count after Series.values:
ls = ['Agree', 'Disagree', 'Neutral', 'N/A'] 

df['key'] = pd.Categorical(df['key'], categories=ls)
#if need order like values in list
#df['key'] = pd.Categorical(df['key'], categories=ls, ordered=True)

df1 = df['key'].value_counts().rename_axis('key').reset_index(name='count')

