I'm trying to count occurrence of each string in a list for each row.
+----+---------------------------+
| Id | Col1 |
+----+---------------------------+
| N1 | ['a', 'b', 'c', 'a'] |
| N2 | ['b', 'b', 'b'] |
| N3 | [] |
| N4 | ['a', 'b', 'c', 'a', 'c'] |
| N5 | [] |
+----+---------------------------+
As a result I want to get something like this:
+----+---------------------------+-----------------------+
| Id | Col1 | Col2 |
+----+---------------------------+-----------------------+
| N1 | ['a', 'b', 'c', 'a'] | {'a':2, 'b':1, 'c':1} |
| N2 | ['b', 'b', 'b'] | {'b':3} |
| N3 | [] | {} or None |
| N4 | ['a', 'b', 'c', 'a', 'c'] | {'a':2, 'b':1, 'c':2} |
| N5 | [] | {} or None |
+----+---------------------------+-----------------------+
Already tried to use Counter from collections library inside DataFrame by different methods, but nothing seems to work.
d = {'Id': ['N1', 'N2', 'N3', 'N4', 'N5'],
'Col1': [['a', 'b', 'c', 'a'], ['b', 'b', 'b'], [], ['a', 'b', 'c', 'a', 'c'], []]}
df = pd.DataFrame(data = d)
