Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

python - Pandas, filter rows which column contain another column

How can I filter rows which column contain another column? For example, if we have DT with two columns A, B, can we filter rows with B.contains(A)? Not just if B contains some A values from all A from DT, but just in one row.

A      B
'lol'  'lolec'
'ram'  'rambo'
'ki'   'pio'

Result:
A     B
'lol'  'lolec'
'ram'  'rambo'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use boolean indexing with mask created by apply and in if need filter columns A and B per rows:

#if necessary strip ' in all values
df = df.apply(lambda x: x.str.strip("'"))
#df = df.applymap(lambda x: x.strip("'"))

print (df.apply(lambda x: x.A in x.B, axis=1))
0     True
1     True
2    False
dtype: bool

df = df[df.apply(lambda x: x.A in x.B, axis=1)]
print (df)
     A      B
0  lol  lolec
1  ram  rambo

Difference of solutions - input DataFrame is changed:

print (df)
     A      B
0  lol    pio
1  ram  rambo
2   ki  lolec

print (df[df.apply(lambda x: x.A in x.B, axis=1)])
     A      B
1  ram  rambo

print (df[df['B'].str.contains("|".join(df['A']))])
    A      B
1  ram  rambo
2   ki  lolec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...