[英]How I can count elements of a column?
Hi I am wondering how I can count elements of each row?嗨,我想知道如何计算每一行的元素?
I have the following column:我有以下专栏:
Column
(‘a’, ‘b’),(‘a’, ‘c’),(‘b’, ‘c’)
(‘g’, ‘h’),(‘a’, ‘c’),(‘a’, ‘b’)
I wanna count how many of the above pair exist in the data set!我想计算数据集中存在多少上述对!
Output:
(‘a’, ‘b’) 2
(‘a’, ‘c’). 2
(‘b’, ‘c’). 1
(‘g’, ‘h’). 1
Use findall
to extract tuple and explode
to separate them.使用
findall
提取元组并explode
以将它们分开。 Finally, use value_counts
to count occurrences:最后,使用
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.