Count Specific Word Across Multiple Columns in Pandas Dataframes, Output Grouped by Column

Trying to find a way to do a sum of all the columns (there are around 7) with the criteria being 1 word? For example, across all the columns of Name, Fruit, Country, etc. I want to know how many times the word ‘the’ appears in each one. 

I can use this df3['Name'].str.count('The').sum(), and that will give this result:

Out[121]: 3522

But then when I add in the next string field so that it is

df3['Name'].str.count('The').sum()
df3['Fruit'].str.count('The').sum()

it only shows the last syntax (as expected): 

Out[122]:27

What I obviously want is for it to say:

Name: 3522
Fruit: 27

But I don’t seem to be able to use str.count or str.contains in a way that groups it like I need. 

If the data is something like the following:

Name | Year | Score | 2nd Score | % of People | Country | Fruit | Export Countries | Language | Transit Duration | Quality | Taste | Freshness | Packaging
Andes, The | 2021 | 8 | 8.8 | 87% | The Netherlands | The Apple | United States,United Kingdom | English,Japanese,French | 148.0 | 1.0 | 0.0 | 0.0 | 0.0
Phil | 2021 | 8 | 8.4 | 87% | Spain | The Banana | United Kingdom, Germany | English,German,French,Italian | 165.0 | 1.0 | 0.0 | 0.0 | 0.0
Sarah | 2021 | 9 | 8.3 | 89% | Greece | The Plum | Germany,United States | English,German,French,Italian | 153.0 | 1.0 | 0.0 | 0.0 | 0.0

The expected output should be

Name: 1
Year: 0
Score: 0
2ndScore: 0
Country: 1
Fruit: 3
TransitDuration: 0
Quality: 0
Taste: 0
Freshness: 0
Packaging: 0

Answer

You could use applymap to get your output; it hits every cell:

In [477]: df.applymap(lambda df: 'The' in df).sum()
Out[477]: 
Name        1
 Fruit      2
 Country    1
dtype: int64

The first part, which is the applymap, returns a series of booleans for each cell in each column:

In [476]: df.applymap(lambda df: 'The' in df)
Out[476]: 
   Name    Fruit    Country
0   True     True      True
1  False     True     False

From here, you can sum the booleans, which is just 1s and 0s

You could use the transform function, or apply to replicate the same result :

 df.transform(lambda df: df.str.contains('The')).sum()
Out[482]: 
Name        1
 Fruit      2
 Country    1
dtype: int64

Based on your comments, you could select only text columns, with the select_dtypes method:

In [483]: df.select_dtypes('object').applymap(lambda df: 'The' in df).sum()
Out[483]: 
Name        1
 Fruit      2
 Country    1
dtype: int64

Thanks to @Shubhamsharma, the solution below works:

 df.astype(str).applymap(lambda s: 'The' in s).sum()