1

I have the following data,

name    marks   team1   team2
John    30  kk  vv.
Sera    56  gg  ww.
Saara   66  dd  gg.
Dirg    33  rr  dd.
maget   34  ff  rr.
fared   56  vv  ff.
juile   32  ww  kk.

I need to have general formula to get records row wise, which have 'kk' in team 1 and team 2 This is just a sample data, I have actual data which is more than 100k rows.

1
  • 1
    try df[(df['team1']=='kk')&(df['team2']=='kk')] Commented Dec 14, 2018 at 6:03

1 Answer 1

1

Use boolean indexing with mask created by filter for get all columns with team, compare by eq (==) and get at least one True per rows by any:

df = df[df.filter(like='team').eq('kk').any(axis=1)]
#if want select columns by names
#df = df[df[['team1','team2']].eq('kk').any(axis=1)]

For better performance use numpy.any:

df = df[np.any(df.filter(like='team').values == 'kk', axis=1)]

print (df)
    name  marks team1 team2
0   John     30    kk    vv
6  juile     32    ww    kk

Details:

print (df.filter(like='team').eq('kk'))
   team1  team2
0   True  False
1  False  False
2  False  False
3  False  False
4  False  False
5  False  False
6  False   True

print (df.filter(like='team').eq('kk').any(axis=1))
0     True
1    False
2    False
3    False
4    False
5    False
6     True
dtype: bool
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.