[英]How I can count elements of a column?
嗨,我想知道如何计算每一行的元素?
我有以下专栏:
Column
(‘a’, ‘b’),(‘a’, ‘c’),(‘b’, ‘c’)
(‘g’, ‘h’),(‘a’, ‘c’),(‘a’, ‘b’)
我想计算数据集中存在多少上述对!
Output:
(‘a’, ‘b’) 2
(‘a’, ‘c’). 2
(‘b’, ‘c’). 1
(‘g’, ‘h’). 1
使用findall
提取元组并explode
以将它们分开。 最后,使用value_counts
计算出现次数:
>>> df.Column.str.findall(r'(\([^()]+\))').explode().value_counts()
(‘a’, ‘b’) 2
(‘a’, ‘c’) 2
(‘b’, ‘c’) 1
(‘g’, ‘h’) 1
Name: Column, dtype: int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.