How to count class label frequency in a data frame using pandas?

Viewed 6658

I have a data frame like this what is the easy method to count class label frequency of a particular class using the panda's data frame.

index  f1 f2 f3 f4 f5 f6  class_label
    0      4  4  2  3  3  1        0
    1      1  4  2  1  3  1        0
    2      4  1  2  1  3  1        0
    3      2  4  1  3  3  1        1
    4      4  4  2  0  3  1        1
    5      3  4  2  4  1  1        1
    6      4  4  2  5  3  1        1
    7      4  4  2  3  3  1        1

I have written down this code but is there any easy way to do this:

import pandas  as pd

df  = pd.read_csv('example.tsv',sep='\t')
class_labels  = df['class_label'].values.tolist()
class_labels_set = set(class_labels)

print class_labels

freq_list = []

for c in class_labels_set:
    freq_list.append(class_labels.count(c))

print 'Freq',freq_list
print 'number',class_labels_set

This code is very slow, on large files

2 Answers

Try using value_counts. It's a useful way Pandas has to compute the frequency count.

As simple as index.value_counts().

I think, I had a similar problem. You want to know, how many lines you have in your dataset with the variable class_label==1 (or any number) - do I get this right?

If so, then I used similarly

df[df.class_label==x]

to give me a sub-array of only the lines, that meet this requirement and just

len(df[df.class_label==x])

to get the number of events, that meet the condition you specified.

Related