Filtering DataFrames in Pandas for multiple columns where a column name contains a pattern
<p>While filtering multiple columns I have seen examples where we could filter Rows using something like this <code>df[df['A'].str.contains("string") | df['B'].str.contains("string")]</code> .</p>

<p>I have multiple files where I want to fetch each file and get only those rows with <code>'gmail.com'</code> from the column names having <code>'email'</code> string in them.  </p>

<p>So an example header can be like: 'firstname' 'lastname' 'companyname' 'address' 'emailid1' 'emailid2' 'emailid3' ...</p>

<p>The columns <code>emailid1..2..3</code> have emailids containing <code>gmail.com</code>. I would want to fetch rows where gmail can occur in any one of them.</p>

<pre><code>for file in files:
    pdf = pd.read_csv('Reduced/'+file,delimiter = '\t')
    emailids = [col for col in pdf.columns if 'email' in col]
    #  pdf['gmail' in pdf[emailids]]
</code></pre>

<p>Given example input of:</p>

<pre><code>df = pd.DataFrame({'email': ['test@example.com', 'someone@gmail.com'], 'somethingelse': [1, 2], 'another_email': ['whatever@example.com', 'something@example.com']})
</code></pre>

<p>eg:</p>

<pre><code>           another_email              email  somethingelse
0   whatever@example.com   test@example.com              1
1  something@example.com  someone@gmail.com              2
</code></pre>

<p>You can filter out the columns that contain email, look for <code>gmail.com</code> or whatever text you wish, then subset, eg:</p>

<pre><code>df[df.filter(like='email').applymap(lambda L: 'gmail.com' in L).any(axis=1)]
</code></pre>

<p>Which gives you:</p>

<pre><code>           another_email              email  somethingelse
1  something@example.com  someone@gmail.com              2
</code></pre>

python pandas indexing filter multiple-columns