如何计算dataframe上列表中的特定字符串元素?

b4qexyjb  于 2021-09-29  发布在  Java
关注(0)|答案(2)|浏览(357)

我有以下清单:

mylist = ['pages', 'disable', 'sensitive', 'application', 'screen', 'login', 'dynamic', 'frida', 'use', 'capture', 'stronger', 'flag_secure', 'strengthen', 'default', 'registration', 'obfuscate', 'anti', 'feature', 'protection', 'blurring', 'appsview', 'instrumentation', 'recent', 'paste', 'copy', 'exported', 'improve', 'mechanism', 'device', 'encryption', 'information', 'version', 'code', 'components', 'restrict', 'access', 'data', 'adding', 'debugger', 'otp', 'runtime', 'server', 'instrument', 'ensure', 'input', 'link', 'special', 'magisk', 'magic', 'obfuscation']

我有一个数据框,其中包含一组字符串:

0                       Implement stronger root detection and adding debugger or dynamic instrument detection at runtime. 
1                                                                 Strengthen root detection and implement Frida detection.
2                                                                           Implement code obfuscation to the application.
3                                                                   Disable screen capture by default and use FLAG_SECURE.
4                                                                            Implement screen blurring on the Recent Apps view.

如何计算数据框上mylist中每个元素的出现次数,并按其值计数对其进行排序?
这就是我希望得到的结果:

Word        Count
pages         31
disable       25
sensitive      6

我怎样才能做到这一点?

qxgroojn

qxgroojn1#

您的预期输出与给定的示例数据不匹配。
首先可以在空间上拆分列,然后 strip 去掉任何剩余的空格或句号和逗号,然后分解它,然后调用 value_countsreindex 在列表中,最后删除 NaN 值,并按降序对值进行排序。
它假定 case-sensitive 计数。


# df is the dataframe, and text is the column name

>>> result=(df['text']
        .str
        .split()
        .apply(lambda x: [i.strip(' .,') for i in x])
        .explode()
        .value_counts()
        .reindex(mylist)
        .dropna()
        .sort_values(ascending=False))

输出:

>>> result
screen         2.0
application    1.0
dynamic        1.0
use            1.0
capture        1.0
stronger       1.0
default        1.0
blurring       1.0
code           1.0
adding         1.0
debugger       1.0
runtime        1.0
instrument     1.0
obfuscation    1.0
Name: text, dtype: float64
gdrx4gfi

gdrx4gfi2#

我希望我已经很好地理解了你的问题。这个例子适用于所有的单词 mylist 并统计 Dataframe 中的出现次数 df ( df["col1"] 您的列是否包含字符串):

df_out = pd.DataFrame({"Word": mylist})
df_out["Count"] = df_out["Word"].apply(
    lambda x: df["col1"]
    .apply(lambda z: sum(x in w for w in z.lower().split()))
    .sum()
)
print(df_out[df_out.Count > 0].sort_values(by="Count", ascending=False))

印刷品:

Word  Count
4        screen      2
1       disable      1
13      default      1
42   instrument      1
40      runtime      1
38     debugger      1
37       adding      1
32         code      1
22       recent      1
19     blurring      1
12   strengthen      1
3   application      1
11  flag_secure      1
10     stronger      1
9       capture      1
8           use      1
7         frida      1
6       dynamic      1
49  obfuscation      1

相关问题