customers= pd.read_csv('marketing_campaign.csv', sep='\t')
customers.head()
marketing_campaign.csv 을 customers라는 이름의 데이터프레임으로 생성customers.info()
# 집계통계 기술
customers.describe().round(1)
# 집계통계 기술 - 숫자 아닌 정보 확인
# 집계통계 기술
customers.describe(exclude=['int','float']).round(1)
# 나이 계산 - 데이터셋이 2021.08.23임
2021 - customers['Year_Birth']
customers['Year_Birth'] 빼기ID
5524 64
2174 67
4141 56
6182 37
5324 40
..
10870 54
4001 75
7270 40
8235 65
9405 67
Name: Year_Birth, Length: 2240, dtype: int64
# 연도 옆에 추가
customers.insert(1, 'Age', 2021 - customers['Year_Birth'])
customers.head()
def classify_age(age):
if age >= 0 and age <= 12:
return 'Child'
elif age >= 13 and age <= 19:
return 'Teenager'
elif age >= 20 and age <= 39:
return 'Young_Adult'
elif age >= 40 and age <= 64:
return 'Middle_aged'
elif age >= 65:
return 'Elderly'
else:
return 'Inavlid_age'
classify_age(9), classify_age(17), classify_age(20), classify_age(59), classify_age(65)
('Child', 'Teenager', 'Young_Adult', 'Middle_aged', 'Elderly')
customers['Age'].apply(classify_age)
ID
5524 Middle_aged
2174 Elderly
4141 Middle_aged
6182 Young_Adult
5324 Middle_aged
...
10870 Middle_aged
4001 Elderly
7270 Middle_aged
8235 Elderly
9405 Elderly
Name: Age, Length: 2240, dtype: object
people_set = {
'Name' : ['Spencer', 'Mark', 'Tom', 'Peter'],
'Major': ['Computer', 'Science', 'English', 'Computer'],
'YearOfJoining' : [2020, 2019, 2018, 2017],
'DriverLicense' : [True, False, False, True],
'TeacherCertification' : [True, False, False, False],
}
| Name | Major | YearOfJoining | DriverLicense | |
|---|---|---|---|---|
| 0 | Spencer | Computer | 2020 | True |
| 1 | Mark | Science | 2019 | False |
| 2 | Tom | English | 2018 | False |
| 3 | Peter | Computer | 2017 | True |
# map() 으로 매핑 확인해서 값 변환
people['DriverLicense'].map({True :'Yes', False: 'No'})
0 Yes
1 No
2 No
3 Yes
Name: DriverLicense, dtype: object
people.applymap(type)
people.applymap(str).applymap(type)
# str같은 경우 Series는 applymap할 필요 없이 가능하나
people['Name'].str.len()
# Dataframe은 .str이 안된다.
people.str()
# 대문자 만드는 함수
def uppercase(data):
return data.upper()
people.applymap(str).applymap(uppercase)