R이야 워낙 많이 써서 익숙한데 Python은 아직 익숙치가 않아서 매번 쓸때마다 찾아봐야하네.
공부겸 정리를 틈틈히 할 필요가 있다.
R에서는 Data.table과 같은 훌륭한 패키지가 있는 것처럼 Python도 pandas가 있다.
나름 고감자님 블로그에서 R로 분석하던 것을 Python으로 조금씩 해볼까 한다.
import pandas as pd
df = pd.read_csv('C:/temp/titanic.csv')
print df.head()
df['isminor'] = 'adult'
df.loc[df.age < 15, 'isminor'] = 'child'
# 과연 이렇게 복잡하게 써야하는 것인가... 공부가 필요하네 그려
df.loc[df.survived == 1,('pclass','survived')].groupby('pclass').agg('count').survived / df.groupby('pclass').agg('count').survived
df.loc[df.survived == 1, ('sex','survived')].groupby('sex').agg('count').survived / df.groupby('sex').agg('count').survived
# 조금 다르게 수정
# 등급별 생존률
survived_pclass_rt = df.loc[df.survived == 1].groupby('pclass')['no'].agg('count') / df.groupby(['pclass'])['no'].agg('count')
print survived_pclass_rt
# 성별 생종률
survived_sex_rt = df.loc[df.survived == 1].groupby('sex')['no'].agg('count') / df.groupby(['pclass'])['no'].agg('count')
print survived_sex_rt
# 등급/성별 생존률
survived_pclass_sex_rt = df.loc[df.survived == 1].groupby(['pclass','sex'])['no'].agg('count') / df.groupby(['pclass','sex'])['no'].agg('count')
print survived_pclass_sex_rt
'Data Analysis > Python' 카테고리의 다른 글
[Python] K-means Clustering (0) | 2020.01.23 |
---|---|
[Python] 다중 회귀 분석(Multiple Linear Regression) (0) | 2019.05.19 |
[Python] jdbc로 Database(Oracle) 접근하기 (2) | 2015.12.03 |
Python : 다중 회귀 분석 (0) | 2015.12.02 |
Python : Numpy Intall 하기 (0) | 2015.11.02 |
댓글