If i have a list of letters:
Out[30]:
LN
0 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
1 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
2 [C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, ...
3 [C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, ...
4 [C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, ...
...
43244 [G, I, O, P, P, P, R, R, R, R]
43245 [G, I, O, P, P, P, R, R, R, R]
43246 [G, I, O, P, P, R, R, R]
43247 [G, I, O, P, P, R, R, R]
43248 [G, I, O, P, R, R]
How can i change it to 0 [C1, C2, C3...C6, G, I, O, P1, P2...]
The reason for this is that networkx will not allow nodes with the same labels, but unfortunately i cannot go and change the raw data, i need to do it here.
You can combine
defaultdict
withitertools.count
to make a simple clean solution. You basically make a counter for each letter in the dict and concat it with the original letter. This should get you started:You can simplify the defaultdict a bit if you don't mind the counts starting at zero:
You can, of course, apply this to a list of lists:
You can also apply this function to a Pandas dataframe using
apply()
with appropriateaxis
andresult_type
parameters:res
will be: