Say I have a dataframe
df = pd.DataFrame({
'column_1': ['ABC DEF', 'JKL', 'GHI ABC', 'ABC ABC', 'DEF GHI', 'DEF', 'DEF DEF', 'ABC GHI DEF ABC'],
'column_2': [9, 2, 3, 4, 6, 2, 7, 1 ]
})
df
column_1 column_2
0 ABC DEF 9
1 GHI ABC 3
2 ABC ABC 4
3 DEF GHI 6
4 DEF 2
5 DEF DEF 7
6 ABC GHI DEF ABC 1
I want to count the number of times each of my regex pattern group is present in the column.
For simplicity, say the pattern is the word ABC and DEF then I need the count of those in all the rows.
Expected output :
column_1 column_2 Group1_count Group2_count
0 ABC DEF 9 1 1
1 JKL 9 0 0
2 GHI ABC 3 1 0
3 ABC ABC 4 2 0
4 DEF GHI 6 0 1
5 DEF 2 0 1
6 DEF DEF 7 0 2
7 ABC GHI DEF ABC 1 2 1
This is what I tried, where I am unable to figure out how to move ahead to get the count value.
df['column_1'].str.extractall('(ABC)|(DEF)').groupby(level=0).first()
0 1
0 ABC DEF
2 ABC None
3 ABC None
4 None DEF
5 None DEF
6 None DEF
7 ABC DEF
A vector solution/one liner approach would be appreciated for this question.
Also note that in this example I have ABC and DEF for simplicity but it could be a complex regex pattern as well.

