擅长:python、mysql、java
<P>考虑^ {CD1>}:</P>
<pre><code>In [1543]: df
Out[1543]:
job_level person_level Employee_type
0 JG05 PG06 RM_type_A
1 JG04 PG04 RM_type_A
2 JG04 PG05 RM_type_B
3 JG03 PG03 RM_type_C
</code></pre>
<p>将<a href="https://www.journaldev.com/20806/python-counter-python-collections-counter" rel="nofollow noreferrer">^{<cd2>}</a>与<a href="https://www.programiz.com/python-programming/dictionary-comprehension" rel="nofollow noreferrer">^{<cd3>}</a>一起使用:</p>
<pre><code>In [1539]: from collections import Counter
In [1537]: x = df.to_dict('list')
In [1544]: res = {k: Counter(v) for k,v in x.items()}
In [1545]: res
Out[1545]:
{'job_level': Counter({'JG05': 1, 'JG04': 2, 'JG03': 1}),
'person_level': Counter({'PG06': 1, 'PG04': 1, 'PG05': 1, 'PG03': 1}),
'Employee_type': Counter({'RM_type_A': 2, 'RM_type_B': 1, 'RM_type_C': 1})}
</code></pre>
<p><code>Counter</code>本身返回一个<code>dict</code></p>