원하는 문자를 한 열의 하위 문자열로 필터링하지만 Python의 전체 문자열은 필터링하지 않습니다.
Nov 16 2020
다음과 같이 작은 데이터 세트가 제공됩니다.
id company
0 1 Reichel-DuBuque
1 2 Jast LLC
2 3 Strosin LLC
3 4 O'Connell, Ortiz and Ullrich
4 5 LLC
5 6 Ltd
6 7 Schoen, Bauch and Breitenberg
7 8 Doyle Ltd
하위 문자열 LLC
이거나 Ltd
전체 문자열이 아닌 행을 필터링하고 싶습니다 . 어떻게 할 수 있습니까? df[df.company.str.contains('LLC|Ltd')]
나에게 준다 :
id company
1 2 Jast LLC
2 3 Strosin LLC
4 5 LLC
5 6 Ltd
7 8 Doyle Ltd
하지만 필요한 것은 다음과 같습니다.
id company
1 2 Jast LLC
2 3 Strosin LLC
7 8 Doyle Ltd
감사합니다.
답변
1 jezrael Nov 16 2020 at 14:00
&
for bitwise 와 같지 않은 테스트를 위해 다른 마스크를 연결할 수 있습니다 AND
.
df = df[df.company.str.contains('LLC') & df.company.ne('LLC')]
print(df)
id company
1 2 Jast LLC
2 3 Strosin LLC
세부 사항 :
print (dd.company.str.contains('LLC'))
0 False
1 True
2 True
3 False
4 True
Name: company, dtype: bool
print (dd.company.ne('LLC'))
0 True
1 True
2 True
3 True
4 False
Name: company, dtype: bool
편집하다:
df = df[df.company.str.contains('LLC|Ltd') & ~df.company.isin(['LLC', 'Ltd'])]
print (df)
id company
1 2 Jast LLC
2 3 Strosin LLC
7 8 Doyle Ltd