python - counting T/F values for several conditions -


i beginner using pandas.

i'm looking mutations on several patients. have 16 different conditions. write code how can loop? try find changes on mut column , set them true , false. try count true/false numbers. have done 4.

can suggest more simple way, instead of writing same code 16 times?

s1=df["mut"] a_t= s1.str.contains("a:t") atnum= a_t.value_counts(sort=true)  s2=df["mut"] a_g=s2.str.contains("a:g") agnum=a_g.value_counts(sort=true)  s3=df["mut"] a_c=s3.str.contains("a:c") acnum=a_c.value_counts(sort=true)  s4=df["mut"] a__=s4.str.contains("a:-") a_num=a__.value_counts(sort=true) 

i'm not expert using pandas, don't know if there's cleaner way of doing this, perhaps following might work?

chars = 'tgc-' nums = {}  char in chars:     s = df["mut"]     = s.str.contains("a:" + char)     num = a.value_counts(sort=true)     nums[char] = num  atnum = nums['t'] agnum = nums['g'] # ...etc 

basically, go through each unique character (t, g, c, -) pull out values need, stick numbers in dictionary. then, once loop finished, can fetch whatever numbers need out of dictionary.


Comments