본문 바로가기
개발일지/Pandas

pandas (판다스) 기초 8 and, or

by 개발에정착하고싶다 2022. 7. 18.
320x100
## Filtering DataFrames with many Conditions (AND)
import pandas as pd

titanic = pd.read_csv('titanic.csv')
titanic.head()
# 남자면서 14살 이상의 사람이 살게된 케이스를 살펴보기위한 필터를 위해서
# 먼저 남자를 지정해준다.
mask_male = titanic.sex == 'male'
mask_male
# 그 다음으로는 14살을 지정해준다.
mask_age = titanic.age > 14
mask_age
(mask_male & mask_age).head()

'''
0     True
1    False
2    False
3    False
4     True
dtype: bool
'''
# pandas(판다스) and 조건으로 출력
# mask_male에 해당하면서 maks_age에 해당하는 열의 survied, pclass, sex, age정보를 알려줘
titanic.loc[mask_male & mask_age, ['survived', 'pclass', 'sex', 'age']]
male_surv = titanic.loc[mask_male & mask_age, ['survived', 'pclass', 'sex', 'age']]
male_surv

'''
	survived	pclass	sex	age
0	0	3	male	22.0
4	0	3	male	35.0
6	0	1	male	54.0
12	0	3	male	20.0
13	0	3	male	39.0
...	...	...	...	...
883	0	2	male	28.0
884	0	3	male	25.0
886	0	2	male	27.0
889	1	1	male	26.0
890	0	3	male	32.0
414 rows × 4 columns
'''
male_surv.info()

'''
<class 'pandas.core.frame.DataFrame'>
Int64Index: 414 entries, 0 to 890
Data columns (total 4 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   survived  414 non-null    int64  
 1   pclass    414 non-null    int64  
 2   sex       414 non-null    object 
 3   age       414 non-null    float64
dtypes: float64(1), int64(2), object(1)
memory usage: 16.2+ KB
'''
male_surv.describe()

'''
survived	pclass	age
count	414.000000	414.000000	414.000000
mean	0.173913	2.309179	33.129227
std	0.379493	0.829868	12.922177
min	0.000000	1.000000	15.000000
25%	0.000000	2.000000	23.000000
50%	0.000000	3.000000	30.000000
75%	0.000000	3.000000	40.000000
max	1.000000	3.000000	80.000000
'''
titanic.describe()

'''
survived	pclass	age	sibsp	parch	fare
count	891.000000	891.000000	714.000000	891.000000	891.000000	891.000000
mean	0.383838	2.308642	29.699118	0.523008	0.381594	32.204208
std	0.486592	0.836071	14.526497	1.102743	0.806057	49.693429
min	0.000000	1.000000	0.420000	0.000000	0.000000	0.000000
25%	0.000000	2.000000	20.125000	0.000000	0.000000	7.910400
50%	0.000000	3.000000	28.000000	0.000000	0.000000	14.454200
75%	1.000000	3.000000	38.000000	1.000000	0.000000	31.000000
max	1.000000	3.000000	80.000000	8.000000	6.000000	512.329200
'''

# 전체적으로 통계적 결과값에 대한 의미를 되짚어보면
# mean에서 0.38이고 std 값이 0.49니깐
# 죽은사람이 보통 38%는 죽었는데, 보통 38%에서 49%더죽거나, 덜죽었다는 의미가될까?
# 즉, 죽은사람의 계층중 가장 심하게 죽은 계층은 87%가 죽었고, 가장 심하게 살아난 계층은 87%가 더 살았다는 것일까?


## Filtering DataFrames with many Conditions (OR)

import pandas as pd

titanic = pd.read_csv('titanic.csv')

# 여자
mask1 = titanic.sex == 'female'
mask1.head()
# 14 미만
mask2 = titanic.age < 14
# mask1 이거나 mask2 면 True를 반환해라.
both = (mask1 | mask2)
titanic.loc[both]

'''
survived	pclass	sex	age	sibsp	parch	fare	embarked	deck
1	1	1	female	38.0	1	0	71.2833	C	C
2	1	3	female	26.0	0	0	7.9250	S	NaN
3	1	1	female	35.0	1	0	53.1000	S	C
7	0	3	male	2.0	3	1	21.0750	S	NaN
8	1	3	female	27.0	0	2	11.1333	S	NaN
...	...	...	...	...	...	...	...	...	...
880	1	2	female	25.0	0	1	26.0000	S	NaN
882	0	3	female	22.0	0	0	10.5167	S	NaN
885	0	3	female	39.0	0	5	29.1250	Q	NaN
887	1	1	female	19.0	0	0	30.0000	S	B
888	0	3	female	NaN	1	2	23.4500	S	NaN
'''
condition = titanic.loc[both, ['survived', 'pclass', 'sex', 'age']]

condition.describe()

'''
survived	pclass	age
count	351.000000	351.000000	298.000000
mean	0.723647	2.205128	25.039161
std	0.447832	0.847232	15.314631
min	0.000000	1.000000	0.420000
25%	0.000000	1.000000	14.125000
50%	1.000000	2.000000	24.000000
75%	1.000000	3.000000	35.000000
max	1.000000	3.000000	63.000000
'''

# 여자이거나 14미만의 살아남은 확률은 약 72%이고
titanic.describe()

'''
survived	pclass	age	sibsp	parch	fare
count	891.000000	891.000000	714.000000	891.000000	891.000000	891.000000
mean	0.383838	2.308642	29.699118	0.523008	0.381594	32.204208
std	0.486592	0.836071	14.526497	1.102743	0.806057	49.693429
min	0.000000	1.000000	0.420000	0.000000	0.000000	0.000000
25%	0.000000	2.000000	20.125000	0.000000	0.000000	7.910400
50%	0.000000	3.000000	28.000000	0.000000	0.000000	14.454200
75%	1.000000	3.000000	38.000000	1.000000	0.000000	31.000000
max	1.000000	3.000000	80.000000	8.000000	6.000000	512.329200
'''

# 전체 승객중에서 살아남은 확률은 약38%정도다

 

300x250