2017-10-03 5 views
1

안녕하세요. 키워드 목록이 있습니다.python에서 pandas를 사용하여 sevaral 키워드와 데이터 프레임 열 값을 매핑하는 방법

keyword_list=['one','two'] 

DF, 

    Name  Description 
    Sri  Sri is one of the good singer in this two 
    Ram  Ram is one of the good cricket player 

내 keyword_list에서 모든 값을 가진 행을 찾고 싶습니다. 내 원하는 출력이

, list comprehension에 의해 생성 된 모든 마스크의

output_Df, 
    Name Description 
    Sri  Sri is one of the good singer in this two 

I tried, mask=DF['Description'].str.contains() method but I can do this only for a single word pls help. 

답변

2

사용 np.logical_and + reduce : 마스크

keyword_list=['one','two'] 

m = np.logical_and.reduce([df['Description'].str.contains(x) for x in keyword_list]) 
df1 = df[m] 
print (df1) 

    Name        Description 
0 Sri Sri is one of the good singer in this two 

대안 :

m = np.all([df['Description'].str.contains(x) for x in keyword_list], axis=0) 

#if no NaNs 
m = [set(x.split()) >= set(keyword_list) for x in df['Description']] 
+1

위의 질문에 대한 솔루션이 작동됩니다 좋아, 제발 내가 좋은 재치가되고 싶어 numpy과 팬더에 대한 최고의 자습서를 제안하시기 바랍니다 수 그거야. – pyd

+0

어려운 질문입니다. 제 생각에는 팬더 문서 및 자습서가 가장 좋습니다. 특히 [현대 팬더] (http://pandas.pydata.org/pandas-docs/stable/tutorials.html#modern-pandas)를 좋아합니다. – jezrael

+0

괜찮 았어, 고맙다. – pyd