[英]Count occurrences in Pandas data frame
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.