<* 간단한 데이터 처리와 분석을 수행하는 예제 *>
◆ Pandas를 이용하여 데이터 프레임을 생성하고, 간단한 데이터 처리와 분석을 수행하는 예제이다.
◆ 이 예제를 확장하여 데이터 전처리, 분석, 시각화 등 다양한 작업을 수행할 수 있다.
1. 데이터 프레임 생성
_________________________________________________________________________________
import pandas as pd
# 데이터 생성 : 딕셔너리
simple_dic= {
'id': ['A001','A003','A002','B003','B001','B002'],
'gender':['F','F','M','F','M','M'],
'wei': 65,66,68,69,71,72],
'hei':[171','172'.'177','176','173'178'],
'age':[23',24','40','38','43','42']
}
# 데이터 프레임 생성
df = pd.DataFrame(simple_dic)
---------------------------------------------------------------------------------------------
#print(df.info()) # 데이터 요약 정보
#print(df.describe()) # 요약 통계
2. 새로운 열 추가
_________________________________________________________________________________
# Pass/Fail 기준 추가
df['old'] = df['age'] >= 41
print(df)
-------------------------------------------------------------------------------------------
출력 :
gender wei hei age old
0 F 65 171 23 False
1 F 66 172 24 False
2 M 68 177 40 False
3 F 69 176 38 False
4 M 71 173 43 True
5 M 72 178 42 True
***< 요약통계 : describe() 예제 >*********************************************
1. 데이터 프레임 생성
______________________________________________________________________________
import pandas as pd
# 데이터 생성 : 딕셔너리
simple_dic= {
'id': ['A001','A003','A002','B003','B001','B002'],
'gender':['F','F','M','F','M','M'],
'wei': 65,66,68,69,71,72],
'hei':[171','172'.'177','176','173'178'],
'age':[23',24','40','38','43','42']
}
# 데이터 프레임 생성
simple = pd.DataFrame(simple_dic)
-----------------------------------------------------------------------------------------
2. 데이터 요약 정보
______________________________________________________
print(df.info())
------------------------------------------------------------
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 gender 6 non-null object
1 wei 6 non-null int64
2 hei 6 non-null int64
3 age 6 non-null int64
dtypes: int64(3), object(1)
memory usage: 324.0+ bytes
None
3. 요약 통계
________________________________________
print(df.describe())
---------------------------------------------
wei hei age
count 6.000000 6.000000 6.000000
mean 68.500000 174.500000 35.000000
std 2.738613 2.880972 9.077445
min 65.000000 171.000000 23.000000
25% 66.500000 172.250000 27.500000
50% 68.500000 174.500000 39.000000
75% 70.500000 176.750000 41.500000
max 72.000000 178.000000 43.000000
***< 데이터 프레임 요약 정보 - info( ) >*********************************
1. 데이터 프레임 생성
________________________________________________________________
import pandas as pd
# 데이터 생성 : 딕셔너리
simple_dic= {
'id': ['A001','A003','A002','B003','B001','B002'],
'gender':['F','F','M','F','M','M'],
'wei': 65,66,68,69,71,72],
'hei':[171','172'.'177','176','173'178'],
'age':[23',24','40','38','43','42']
}
# 데이터 프레임 생성
simple = pd.DataFrame(simple_dic)
------------------------------------------------------------------------
2. 데이터 요약 정보
________________________________________________________________
print(df.info())
------------------------------------------------------------------------
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 6 entries, 0 to 5
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 gender 6 non-null object
1 wei 6 non-null int64
2 hei 6 non-null int64
3 age 6 non-null int64
dtypes: int64(3), object(1)
memory usage: 324.0+ bytes
None
***< 데이터 프레임 필터링 및 정렬하기 >****************************
1. 데이터 프레임 생성
___________________________________________________________________
import pandas as pd
# 데이터 생성 : 딕셔너리
simple_dic= {
'id': ['A001','A003','A002','B003','B001','B002'],
'gender':['F','F','M','F','M','M'],
'wei': 65,66,68,69,71,72],
'hei':[171','172'.'177','176','173'178'],
'age':[23',24','40','38','43','42']
}
# 데이터 프레임 생성
df = pd.DataFrame(simple_dic)
df
----------------------------------------------------------------------------
결과 :
gender wei hei age
0 F 65 171 23
1 F 66 172 24
2 M 68 177 40
3 F 69 176 38
4 M 71 173 43
5 M 72 178 42
2. 데이터 필터링 및 정렬
__________________________________________________________________
# 나이가 30세 이상인 데이터 필터링
filtered = df[df['age'] >=30]
print(filtered)
--------------------------------------------------------------------------
gender wei hei age
2 M 68 177 40
3 F 69 176 38
4 M 71 173 43
5 M 72 178 42
__________________________________________________________________
# 점수 기준으로 내림차순 정렬
sorted_df = df.sort_values(by='hei', ascending=False)
print(sorted_df)
----------------------------------------------------------------------------
출력:
gender wei hei age
5 M 72 178 42
2 M 68 177 40
3 F 69 176 38
4 M 71 173 43
1 F 66 172 24
0 F 65 171 23
***< 그룹별(남녀별) 몸무게 평균값 구하기 - df.groupby('gender')['wei'].mean() >********
1. 데이터 프레임 생성
____________________________________________________________________
import pandas as pd
# 데이터 생성 : 딕셔너리
simple_dic= {
'id': ['A001','A003','A002','B003','B001','B002'],
'gender':['F','F','M','F','M','M'],
'wei': [65,66,68,69,71,72],
'hei':[171,172,177,176,173,178],
'age':[23,24,40,38,43,42]
}
}
# 데이터 프레임 생성
df = pd.DataFrame(simple_dic)
----------------------------------------------------------------------------
#print(df.info()) # 데이터 요약 정보
#print(df.describe()) # 요약 통계
2. 그룹화 및 집계
____________________________________________________________________
df.groupby('gender')['wei'].mean()
----------------------------------------------------------------------------
gender
F 66.666667
M 70.333333
Name: wei, dtype: float64