Frequency Tables and Writing CSVs

What is Frequency?

Frequency: The number of times a value occurs within the data.

candybars_mini
name weight available_canada_america
0 Coffee Crisp 50 Canada
1 Butterfinger 184 America
2 Skor 39 Both
... ... ... ...
4 Twix 58 Both
5 Reeses Peanutbutter Cups 43 Both
6 3 Musketeers 54 America

7 rows × 3 columns


What is a Frequency Table?

available_canada_america
Both       3
Canada     2
America    2
Name: count, dtype: int64
mfr_column = cereal['mfr']
mfr_column
0     N
1     Q
2     K
     ..
74    R
75    G
76    G
Name: mfr, Length: 77, dtype: object


mfr_freq = mfr_column.value_counts()
mfr_freq
mfr
K    23
G    22
P     9
     ..
Q     8
N     6
A     1
Name: count, Length: 7, dtype: int64
mfr_col_dataframe = cereal[['mfr']]
mfr_col_dataframe
mfr
0 N
1 Q
2 K
... ...
74 R
75 G
76 G

77 rows × 1 columns


mfr_col_dataframe.value_counts()
mfr
K      23
G      22
P       9
       ..
Q       8
N       6
A       1
Name: count, Length: 7, dtype: int64

Saving a dataframe

mfr_freq.to_csv('data/mfr_frequency.csv', index=False)

Let’s apply what we learned!