The core issue in solving this problem is that each cell contains a string value, so any pandas opertaions will consider the whole cell as a single unit. To remedy this, it is necessary to transform the data so that each cell is a single-character string.
There are many approaches with varying efficiencies, but they all fundamentally rely on turning a column of strings into a column of single-character strings.
- Creating a
chain.from_interable then applying Series.value_counts:
from itertools import chain
import pandas as pd
char_list = ['LNTS', 'AGTS', 'LMFS', 'PGST']
output = pd.Series(chain.from_iterable(char_list)).value_counts()
map each string into a list, then Series.explode and value_counts:
import pandas as pd
char_list = ['LNTS', 'AGTS', 'LMFS', 'PGST']
output = pd.Series(map(list, char_list)).explode().value_counts()
Series.apply or Series.map list then explode and value_counts:
import pandas as pd
char_list = ['LNTS', 'AGTS', 'LMFS', 'PGST']
output = pd.Series(char_list).apply(list).explode().value_counts()
import pandas as pd
char_list = ['LNTS', 'AGTS', 'LMFS', 'PGST']
output = pd.Series(char_list).map(list).explode().value_counts()
All options produce:
output:
S 4
T 3
L 2
G 2
N 1
A 1
M 1
F 1
P 1
dtype: int64
Some timing information via %timeit:
%timeit pd.Series(chain.from_iterable(char_list)).value_counts()
273 µs ± 10.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit pd.Series(map(list, char_list)).explode().value_counts()
358 µs ± 12.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit pd.Series(char_list).map(list).explode().value_counts()
428 µs ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit pd.Series(char_list).apply(list).explode().value_counts()
508 µs ± 36.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
From @ALollz's answer:
%timeit pd.Series(Counter(chain.from_iterable(char_list))).sort_values(ascending=False)
215 µs ± 7.61 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit pd.Series(char_list).str.split('').str[1:-1].explode().value_counts()
677 µs ± 14.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)