본문 바로가기
python/pandas

pandas - 컬럼 값에 포함되지 않는 경우의 갯수 찾기 ' ~ '

by leo104 2022. 11. 29.
728x90

 컬럼 값에 포함되지 않는 경우

 

df의 데이터 중 choice_description 값에 Vegetables 들어가지 않는 경우의 갯수를 출력하라

df.head()

	order_id	quantity	item_name	choice_description	item_price	new_price
0	1	1	Chips and Fresh Tomato Salsa	NoData	$2.39	2.39
1	1	1	Fizzy Lizzy	[Clementine]	$3.39	3.39
2	1	1	Nantucket Nectar	[Apple]	$3.39	3.39
3	1	1	Chips and Tomatillo-Green Chili Salsa	NoData	$2.39	2.39
4	2	2	Chicken Bowl	[Tomatillo-Red Chili Salsa (Hot), [Black Beans...	$16.98	16.98

일 때,

우선 포함하는 경우를 찾는다.

df['choice_description'].str.contains('Vegetables' , case = False)

0       False
1       False
2       False
3       False
4       False
        ...  
4617    False
4618    False
4619     True
4620     True
4621     True

이 값에서 True와 False를 맞바꾸면 True 값이 Vegetables를 포함하지 않는 경우다.
이때 제일 앞에 ~ 를 넣어준다.

~df['choice_description'].str.contains('Vegetables' , case = False)

0        True
1        True
2        True
3        True
4        True
        ...  
4617     True
4618     True
4619    False
4620    False
4621    False
Name: choice_description, Length: 4622, dtype: bool

이렇게 Vegetables를 포함하지 않는 경우가 True로 출력된다.

 

728x90