如何获取pandas数据帧中不唯一且按行的项目数?

Cur*_*ude 1 python pandas

我发现的大多数解决方案都是在数据框中获取唯一项目(我不想要的),或者按明确标题的列计数.

我的数据框如下:

       1   2   3   4
ILLU1 ATG --T --- TGG
ILLU2 ATG -CT GGG TGG
ILLU3 ATG TTT AAA TGG
ILLU4 -TG --T --- T-G
Run Code Online (Sandbox Code Playgroud)

我试图获得每行的计数,其中每列具有完整的3碱基序列.因此,如果单元格中存在" - ",则它将为零(这包括"---"," - N"," - NN").

所以我想尝试输出如下:

ILLU1 2
ILLU2 3
ILLU3 4
ILLU4 0
Run Code Online (Sandbox Code Playgroud)

我试过了:

df_new = pd.DataFrame() # to hold the final values
count = 0
for rows in df:
   if not sum(df[rows].str.contains("-")) > 0: # if no hyphen present
       count += 1 # add to final count
   else:
       count = count # does not get included final count
       df_new["Final Count"] = count 

print(df_new)
Run Code Online (Sandbox Code Playgroud)

但我只得到一个没有值的空数据帧.

WeN*_*Ben 5

运用 str.contains

s=(~df.apply(lambda x : x.str.contains('-'))).sum(1)
s
Out[384]: 
ILLU1    2
ILLU2    3
ILLU3    4
ILLU4    0
dtype: int64
Run Code Online (Sandbox Code Playgroud)