简体   繁体   English

计算熊猫数据框中的出现次数

[英]Count occurrences in Pandas data frame

I have the following data frame: 我有以下数据框:

在此处输入图片说明

I'm looking to come up with this data frame: 我正在寻找这个数据框架: 在此处输入图片说明

which is counting the occurrences of pipe delimited strings in position and type column. 它计算位置和类型列中管道分隔字符串的出现次数。

The trick is to use collections.Counter 诀窍是使用collections.Counter

In [1]: from collections import Counter
In [2]: s = pd.Series(["AAA|BBB"])
In [3]: s.str.split("|").apply(Counter).apply(pd.Series)
Out[3]:    
   AAA  BBB
0    1    1

Though, you might also want to rename and concat them (assuming your DataFrame is called df ): 不过,您可能还想重命名并DataFrame它们(假设您的DataFrame称为df ):

# Counting
positions = df["POSITION"].str.split("|").apply(Counter).apply(pd.Series)
types = df["TYPE"].str.split("|").apply(Counter).apply(pd.Series)

# Tidying
positions = positions.fillna(0).add_suffix("_CNT")
types = types.fillna(0).add_suffix("_CNT")

# Joining
df = pd.concat([df, positions, types], axis=1)

you could split each value and then apply count method. 您可以拆分每个值,然后应用计数方法。 see example below 见下面的例子

df  = pd.DataFrame.from_dict({'POSITION':['FRONT|FRONT|BACK|BACK|BACK'], 'TYPE': ['EXIT|EXIT|EXIT|WINDOW']})

df = df.assign(EXIT_CNTR = lambda x: x.TYPE.apply(lambda y: y.split('|').count('EXIT')))
df = df.assign(WINDOW_CNTR = lambda x: x.TYPE.apply(lambda y: y.split('|').count('WINDOW')))
df = df.assign(FRONT_CNTR = lambda x: x.POSITION.apply(lambda y: y.split('|').count('FRONT')))
df = df.assign(BACK_CNTR = lambda x: x.POSITION.apply(lambda y: y.split('|').count('BACK')))

results in 结果是

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM