0

Suppose I want to find the number of occurrences of something in a pandas dataframe as one number. If I do df.isin(["ABC"]).sum() it gives me a table of all occurrences of "ABC" under each column.

What do I do if I want just one number which is the number of "ABC" entries under column 1?

Moreover, is there code to find entries that have both "ABC" under say column 1 and "DEF" under column 2. even this should just be a single number of entries/rows that have both of these.

coolguy
  • 21
  • 3

2 Answers2

0

You can check with groupby + size

out = df.groupby(['col1', 'col2']).size()
print(out.loc[('ABC','DEF')])
BENY
  • 296,997
  • 19
  • 147
  • 204
0

Q1: I'm sure there are more sophisticated ways of doing this, but you can do something like:

num_occurences = data[(data['column_name'] == 'ABC')]
len(num_occurences.index)

Q2: To add in 'DEF' search, you can try

num_occurences = data[(data['column_name'] == 'ABC') & (data['column_2_name'] == 'DEF')]
len(num_occurences.index)

I know this works for quantitative values; you'll need to see with qualitative.

Broski-AC
  • 727
  • 4
  • 11