๊ทธ๋ฃน์ ์ง์ด์ ํน์ ๊ด์ ์ ๊ฐ์ง๊ณ ๋ณด๊ฒ ๋ค๋ ๋ป
df.groupby('host_name').size().sort_index()
Ans = df.host_name.value_counts().sort_index()
Ans.head()
- Ans = df.groupby('host_name').size().sort_index()
- Ans = df.host_name.value_counts().sort_index()
โช๏ธ
1. value_counts()
- host_name์ด ๊ฐ์ง๊ณ ์๋ ์ ๋ํฌํ ๊ฐ์๋ฅผ ์ธ์ค๋ค. ๊ฐ์๊ฐ ๋ง์ ์์๋๋ก
- ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ์ ํด์ค๋ค.
- host_name์ด ๊ฐ์ง๊ณ ์๋ ์ ๋ํฌํ ๊ฐ์๋ฅผ ์ธ์ค๋ค.
- ๊ฐ์๊ฐ ๋ง์ ์์๋๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ์ ํด์ค๋ค.
- ๋๊ฐ์ ์ธ์ฃผ์ง๋ ์๋๋ค.
ํ์ง๋ง dropna=False ๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๊ฐ๋ ๊ฐ์ด ์ธ์ค๋ค.
- size() - ๋๊ฐ์ด ์์ด๋ ์ธ์ค๋ค.
๐๊ทธ๋ฃนํ์ ํ ์ ๋ค์ ๋๋ถ๋ถ ์ธ๋ฑ์ค๋ก ์ ์ธ์ด ๋๋ค.
Ans = df.groupby('host_name').size().\
to_frame().rename(columns={0:'counts'}).\
sort_values('counts',ascending=False)
Ans.head(5)
โช๏ธ ์ฝ๋๊ฐ ๋๋ฌด ๊ธธ๋ '\'๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ์ํฐ๋ก ์ธ์์ ํด์ ์ธ ์ ์๋ค.
to_frame() -๋ฐ์ดํฐ ํ๋ ์ํ ํ๊ฒ ๋ค๋ ์ด์ผ๊ธฐ
rename(columns={0:'counts'})
- 0์ count๋ผ๋ name์ผ๋ก
๋ณ๊ฒฝ์ ํ ๊ฒ์ด๋ค.
sort_values('counts',ascending=False)
- count๋ผ๋ ๊ฒ์
๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์df.groupby('host_name').size()
df.host_name.value_counts().to_frame()
type(Ans)
pandas.core.series.Series
โช๏ธ ์ฌ์ค ๋ฐ์ดํฐ ํ๋ ์์ด ์๋ ์๋ฆฌ์ฆ์ด๋ค.
Ans = df.groupby(['neighbourhood_group','neighbourhood'], as_index=False).size()
Ans.head()
โช๏ธ
1. groupby - ๋๊ฐ์ ๊ธฐ์ค์ผ๋ก grouping์ ํ ์๋ ์๋ค.
2.as_index=False
- ์๋ฆฌ์ฆ์ฌ์ ๋ฐ์ดํฐ ํ๋ ์ํ๋ก ๋ณ๊ฒฝํด์ฃผ๊ธฐ ์ํด์
์ด ๋ช ๋ น์ด๋ฅผ ์ด๋ค.โช๏ธ ์ธ๋ฑ์ค๋ก ์ค์ ๋๋ ๊ฒ์ด ์๋ ํ๋์ ์ปฌ๋ผ์ผ๋ก ์ค์ ๋์ ์กฐ๊ธ ๋ ์๊ฐ์ ์ผ๋ก ์ง๊ด์ ์ผ๋ก ๋ณผ ์ ์๊ฒ ๋๋ค.
Ans = df.groupby('neighbourhood_group')['reviews_per_month'].agg(['mean','var','max','min'])
Ans
โช๏ธ
agg() - ์ฌ์น ์ฐ์ฐ์ ์ด๋ค ๊ฑธ ํด์ค์ง์ ๋ํด ์ ์ธ์ ํด์ค ๊ฒ์ด๋ค./ ํ๋ฒ์ ์ฌ๋ฌ๊ฐ์ง ์ฌ์น์ฐ์ฐ์ ํ ์ ์๋ค.
- ๊ณ์ธต์ indexing ์์ด ๊ตฌํ๋ผ-> ๊ณ์ธต์ ์ผ๋ก ๋ณด์ด๋ ๊ฒ์ ์ข ๋ ์ง๊ด์ ์ผ๋ก ๋ง๋ค์ด๋ฌ๋ผ๋ ๋ป
- fillna(-999) - ๋น ๊ฐ์ด ์์ผ๋ฉด -999๋ก ์ฑ์๋ฃ๊ฒ ๋ค๋ ๋ง
Ans = df[['neighbourhood_group','room_type']].groupby(['neighbourhood_group','room_type']).size().unstack()
Ans.loc[:,:] = (Ans.values /Ans.sum(axis=1).values.reshape(-1,1))
Ans
โช๏ธ
1.[['neighbourhood_group','room_type']
์ด ๋ฐ์ดํฐ๋ค์ ๊ฐ์ ธ์์groupby( ['neighbourhood_group','room_type']
๋ฅผ ์์ด ๊ฒ์ด๋ค.
2.size().unstack()
- ์ซ์๋ฅผ ์ธ๊ณ ํ์ด์ค๋ค.
3.Ans.loc[:,:]
- ๋ชจ๋ ๊ฐ์ ๊ฐ์ ธ์จ๋ค.
Ans.values
โช๏ธ Ans.values
- ๊ฐ ๊ฐ์ ๋ํ array๋ฅผ Matrix ํํ๋ก ๋ฝ์์ค๋ค.
Ans.sum(axis=1)
โช๏ธ Ans.sum(axis=1)
- ์ปฌ๋ผ๊ฐ์ ๊ธฐ์ค์ผ๋ก sum์ ํ๊ฒ ๋ค๋ ๋ง
Ans.sum(axis=1).values
**Ans.values /Ans.sum(axis=1).values.reshape(-1,1)**
โช๏ธ
1.Ans.values
๋ฅผAns.sum(axis=1).values.reshape(-1,1)
๋ก
๋๋ ์ค ๊ฒ์ด๋ค.
2.values.reshape(-1,1)
๋ก ๋ฐ์ดํฐ ํํ๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ ๊ฐ์ ํํ๋ผ๋ฆฌ ๋๋ ์ค ์ ์๊ฒ ํ๋ค.
dic = {
'Unknown' : 'N',
'Less than $40K' : 'a',
'$40K - $60K' : 'b',
'$60K - $80K' : 'c',
'$80K - $120K' : 'd',
'$120K +' : 'e'
}
df['newIncome'] = df.Income_Category.map(lambda x: dic[x])
Ans = df[['newIncome', 'Income_Category']]
Ans.head()
โช๏ธ
1. dic - dictionary / ํ์์ ๋ฐ์ดํฐ๋ก ์ด๋ฃจ์ด์ง
2.Income_Category
์ด ๋ณ์๋ฅผ x๋ก ๋ณด๋ ๊ฒ์ด๋ค.
3. dic[x]- ์์ ์๋ dictionary์ income ์นดํ ๊ณ ๋ฆฌ๋ฅผ ๋ฃ์ด์ฃผ๊ฒ ๋ค๋ ๋ป
๐
1. ๋๋ค(lambda) ํจ์๋ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์์ ์ค์ํ ๊ฐ๋ ์ค ํ๋๋ก, ์ต๋ช ํจ์(anonymous function)๋ผ๊ณ ๋ ๋ถ๋ฆ ๋๋ค.
2. ๋๋ค ํจ์๋ ์ด๋ฆ์ด ์๋ ํจ์๋ก, ์ผ๋ฐ์ ์ผ๋ก ํจ์๋ฅผ ํ ๋ฒ๋ง ์ฌ์ฉํ๊ฑฐ๋ ํจ์๋ฅผ ์ธ์๋ก ์ ๋ฌํด์ผ ํ๋ ๊ฒฝ์ฐ์ ๋งค์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ฉ๋๋ค.
3. apply()๋ ์ฌ์ฉ์ ์ ์ํ ํ๋์ ๋ฐ์ดํฐ ํ๋ ์ ์๋ฆฌ์ฆ์ ์ ์ฉํ ๋
๋ง์ด ์ด๋ค.
4. def - ๋ด๊ฐ ์ ์๋ฅผ ๋ง๋ค์ด์ ์ฐ๊ณ ์ถ์ ๋ ์ฐ๋ ๊ฒ
df['AgeState'] = df.Customer_Age.map(lambda x: x//10 *10)
Ans = df['AgeState'].value_counts().sort_index()
Ans
โช๏ธ
1.Customer_Age
- ์ฐ์ํ ์ซ์ํ์ ๋ณ์
2.map(lambda x
- ๋งตํ์ ๋ณ๊ฒฝ์ ํด์ค๋ค๋ ๋ง
3. x//10 - ์ฌ๊ธฐ์ //๋ ๋๋์ ํ ๋ชซ์ ๋ํ๋ด์ค๋ค.49//10*10
40
df['newEduLevel'] = df.Education_Level.map(lambda x : 1 if 'Graduate' in x else 0)
Ans = df['newEduLevel'].value_counts()
Ans
โช๏ธ
1.if 'Graduate' in x else 0
- if else์ ์ถ์ฝ๋ฌธ์ผ๋ก ๋ณผ ์ ์๋ค.
2.map(lambda x : 1 if 'Graduate' in x else 0)
- ๋๋ฒ์งธ x์ Graduate๊ฐ ์์ผ๋ฉด 1์ ๋ด๋ฑ๊ณ ์์ผ๋ฉด 0์ ๋ด๋ณด๋ธ๋ค.
์ฌ๊ธฐ์ map ์ apply๋ก ๋ฐ๊ฟ๋ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์จ๋ค.
import numpy as np
df['newEduLevel'] = np.where( df.Education_Level.str.contains('Graduate'), 1, 0)
Ans = df['newEduLevel'].value_counts()
Ans
โช๏ธ
1.np.where( df.Education_Level.str.contains('Graduate'), 1, 0)
โช๏ธ np.where์ ์์ ์๋ ์กฐ๊ฑด
(df.Education_Level.str.contains('Graduate'))
์ ๋ง์กฑํ๋ฉด 1 ์๋๋ฉด 0์ผ๋ก ๋ณํํ๋ผ๋ ์๋ฏธ์ด๋ค.
๐ ๋ฐ๋ก ์์ ๋๊ฐ์ ์ฝ๋์ฒ๋ผ ๋์ผํ ๊ธฐ๋ฅ์ ์ํํ๋๋ฐ ๋ค์ํ ๊ธฐ๋ฅ์ ํตํด์ ๋ค์ํ ๋ช ๋ น์ด๋ฅผ ํตํด์ ๊ฒฐ๊ณผ๋ฅผ ๋ด๋ฑ์ ์ ์๋ค.
def check(x):
if x.Marital_Status =='Married' and x.Card_Category =='Platinum':
return 1
else:
return 0
df['newState'] = df.apply(check,axis=1)
Ans = df['newState'].value_counts()
Ans
โช๏ธ
1. map๋ณด๋จ ์ฌ์ฉ์ ์ ์ํจ์๋ฅผ ๋ง๋ค์ด ๋๊ณ apply๋ก ์ ์ํ๋ ๊ฒ์ด ์ข๋ค.
2.axis=1
-ํ๊ธฐ์ค์ผ๋ก ์ถ๋ ฅ์ด๋ ์ด๊ธฐ์ค์ผ๋ก ์ถ๋ ฅ์ด๋ ์ ํด์ผ๋๋๋ฐ ์ด๊ฑด ์ด๊ธฐ์ค์ผ๋ก ์ถ๋ ฅํ๋ค.
๐ ์ฐธ๊ณ
df['Gender'] = df.Gender.apply(changeGender)
โช๏ธ gender๋ผ๋ ํ๋์ ์ ์ฉํ๋ฏ๋ก ์ถ๊ธฐ์ค์ ์์ ํด์ค๋ ๋๋ค.