pandas 数据帧列中的 Python 计数字符串(字)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42505790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python count string (word) in column of a dataframe
提问by Damian
i have the following dataframe (df_hvl) with the columnname "FzListe" and the following data:
我有以下数据框(df_hvl),列名“FzListe”和以下数据:
FzListe
7MA1, 7OS1
7MA1, 7ZJB
7MA2, 7MA3, 7OS1
76G1, 7MA1, 7OS1
7MA1, 7OS1
71E5, 71E6, 7MA1, FSS1
71E4, 7MA1, 7MB1, 7OS1
71E6, 7MA1, 7OS1
7MA1
7MA1, 7MB1, 7OS1
7MA1
7MA1, 7MA2, 7OS1
04, 7MA1
76G1, 7MA1, 7OS1
76G1, 7MA1, 7OS1
7MA1, 7OS1
7MA1
76G1, 7MA1, 7OS1
76G1, 7MA1, 7OS1
71E6, 7MA1
7MA1, 7MA2, 7OS1
7MA1
7MA1
7MA1
7MA1, 7OS1
76G1, 7MA1
I want to search for the string "7MA" only and count how often it appears in the list. (The list is originally much longer than that snippet). I want not to search only for 7MA1 because its possible that in one line it appears also with 7MA2 and/or 7MA3 and so on...
我只想搜索字符串“7MA”并计算它在列表中出现的频率。(该列表最初比该片段长得多)。我不想只搜索 7MA1,因为它可能在一行中出现 7MA2 和/或 7MA3 等等......
The Dataframe is called df_hvl and i searched for a solution but didnt find one.
Dataframe 被称为 df_hvl,我搜索了一个解决方案,但没有找到。
Thanks for your help
谢谢你的帮助
回答by jezrael
I think you need str.count
with sum
:
我认为你需要str.count
有sum
:
print (df_hvl.FzListe.str.count(substr))
0 1
1 1
2 2
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 2
12 1
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 2
21 1
22 1
23 1
24 1
25 1
Name: FzListe, dtype: int64
substr = '7MA'
print (df_hvl.FzListe.str.count(substr).sum())
29
回答by Mayeul sgc
I would try something like this I think
我想尝试这样的事情
b=0
for index in df.index:
A=df.loc[row,'FzList'].split(',')
for element in A:
if '7MA'in element:
b+=1
return b
回答by Nouh ABA
this will work too probably
这很可能会奏效
df_hvl.FzListe.map(lambda d: "7MA" in d).sum()