1

I have Data frame in which I want to count occurrence of some word per each column. Per one coumn I can do:

df['Col1'].str.contains('test').value_couns()

or

df[df['Col1'].str.contains('test')]['Col1'].count()

and i get count for particular column.

How can I get it for all columns? I would like to avoid do it manually per each column since there are quite a few of them.

enter image description here

Expected output

enter image description here

Submi
  • 101
  • 1
  • 8

2 Answers2

1

one way to solve this, As Submi Tried,

print (df.astype(str).apply(lambda x: x.str.contains('test').value_counts()).loc[True].fillna(0)).to_frame().T.reset_index(drop=True)

Output:

   col1  col2  col3
0   1.0   0.0   2.0
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111
  • First option I would like to avoid, I have a lot of dfs and a lot of different columns in those. Second option works. only difference with @theletz solution is floats instead of int but this is not a problem. Thank you. – Submi Jun 22 '18 at 07:52
  • @Submi - To get exactly same output use reset_index. updated to the solution – Mohamed Thasin ah Jun 22 '18 at 07:57
1

I think you are looking for this:

df.applymap(lambda x: 'test' in str(x)).sum()
theletz
  • 1,713
  • 2
  • 16
  • 22