Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python count string (word) in column of a dataframe

Tags:

python

pandas

i have the following dataframe (df_hvl) with the columnname "FzListe" and the following data:

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...

The Dataframe is called df_hvl and i searched for a solution but didnt find one.

like image 899
Damian Avatar asked Dec 18 '22 09:12

Damian


1 Answers

I think you need str.count with 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
like image 133
jezrael Avatar answered Jan 27 '23 22:01

jezrael