Populating a dict with a list in the same loop

  • A+
Category:Languages

I am trying to populate a dict with column-wise occurences of characters in pandas sereis. The sereis is as follows:

>>> jkl 1     ATGC 2     GTCA     3     CATG Name: 0, dtype: object 

I want a dict in a way that contains all the characters as keys and list of their column-wise occurence frequencies as value for the dict as shown below:

{'A':[1,1,0,1],'C':[1,0,1,1],'G':[1,0,1,1],'T':[0,2,1,0]} 

I have tried several codes and this is one of them:

mylist = ['A', 'C', 'G','T'] dict = {key: None for key in mylist} for i,(a,b) in enumerate(zip_longest(jkl[1],dict.keys())):     t=str(list(jkl.str[i]))     single_occurrences = Counter(t)         kl.append(single_occurrences.get(b))     dict[b]=kl 

But this dict does not contain the desired output, is there a solution?

 


Counter

from collections import Counter  pd.Series(Counter(     (c, i) for i, C in enumerate(zip(*jkl)) for c in C) ).unstack(fill_value=0)     0  1  2  3 A  1  1  0  1 C  1  0  1  1 G  1  0  1  1 T  0  2  1  0 

pd.Series(Counter(     (c, i) for i, C in enumerate(zip(*jkl)) for c in C )).unstack(fill_value=0).T.to_dict('l')  {'A': [1, 1, 0, 1], 'C': [1, 0, 1, 1], 'G': [1, 0, 1, 1], 'T': [0, 2, 1, 0]} 


np.add.at

Completely different tack

r, i = np.unique([*''.join(jkl)], return_inverse=True) n, m = len(r), len(jkl) j = np.tile(np.arange(n), m) a = np.zeros((n, n), int) np.add.at(a, (i, j), 1) 

DataFrame

pd.DataFrame(a, r)     0  1  2  3 A  1  1  0  1 C  1  0  1  1 G  1  0  1  1 T  0  2  1  0 

Dictionary

dict(zip(r, a.tolist()))  {'A': [1, 1, 0, 1], 'C': [1, 0, 1, 1], 'G': [1, 0, 1, 1], 'T': [0, 2, 1, 0]} 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: