2014-02-28 4 views
2

다음과 같은 목록의 요소를 기반으로 데이터 프레임을 필터링 할 수 있습니다.Python Pandas : 정규식을 적용하여 데이터 프레임 필터링

import pandas as pd 
W1 = ['Animal','Ball','Cat','Derry','Element','Lapse','Animate this'] 
W2 = ['Krota','Catch','Yankee','Global','Zeb','Rat','Try'] 
df = pd.DataFrame({'W1':W1,'W2':W2}) 

l1 = ['Animal','Zeb','Q'] 
print df[df['W1'].isin(l1) | df['W2'].isin(l1)] 

     W1  W2 
    0 Animal Krota 
    4 Element Zeb 

그러나 정규 표현식을 적용하여 필터링 할 수있는 방법이 있습니다. 예;

l1 = ['An','Cat'] 

Intended result; 
      W1   W2 
    0 Animal  Krota 
    1 Ball   Catch 
    2 Cat   Yankee 
    6 Animate this Try 

답변

5

이 시도 :

df[df['W1'].str.contains("|".join(l1)) | df['W2'].str.contains("|".join(l1))] 


      W1  W2 
0  Animal Krota 
1   Ball Catch 
2   Cat Yankee 
6 Animate this  Try 
관련 문제