简体   繁体   中英

Count Partial match in each DataFrame Column in Pandas

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.

在此处输入图片说明

Expected output

在此处输入图片说明

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

我认为您正在寻找:

df.applymap(lambda x: 'test' in str(x)).sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM