지금은 Excel 파일에 설명 된 특정 기준에 따라 행을 제거하는 함수를 만들려고합니다.CSV 파일 (Pandas, Python3)을 기반으로 DF 행 제거
header
the man
is a
내 코드의 두 번째 부분은 내가
import pandas as pd
data = ({'words':['the man','is a','good guy']})
df = pd.DataFrame(data)
xl = pd.ExcelFile('C:/Users/j/Desktop/bad words2.xlsx')
badwords = xl.parse()
badwords = badwords['header']
def removewords(x):
for w in x:
pattern = '^'+''.join('(?=.*{})'.format(word) for word in w.split())
df[df['words'].str.contains(pattern)==False]
df.dropna()
print(removewords(badwords))
을 적용하려고 함수 :이 엑셀 파일 (나쁜 words2)는 DF 내에서 제거해야 단어 쌍을 포함하고 있으며 다음과 같습니다 이상적으로,이 기능을 적용의 끝에서, 나는 단지가 포함 된 DF와 끝까지해야합니다
words
good guy
그러나 지금,이 함수가 반환하는 모든 것을 '없음'입니다. 내가 도대체 뭘 잘못하고있는 겁니까?
죄송합니다 당신은 단지 패턴에 단어를 필터링하거나 패턴에없는 단어를 필터링하고 싶은거야? 어쨌든 당신이하려는 일이 불분명하지만이 줄은 할당 없이는 아무 일도하지 않습니다 :'df [df [ 'words'] .str.contains (pattern) == False]''df = df [df [ 'words' ] .str.contains (pattern) == False]' – EdChum