Pandas 기초 -2

조은지·2021년 4월 25일
0

Summary Functions and Maps

1. Summary functions

reviews.points.describe()
out:
count    129971.000000
mean         88.447138
             ...      
75%          91.000000
max         100.000000
Name: points, Length: 8, dtype: float64

-> numerical data

reviews.taster_name.describe()
out:
count         103727
unique            19
top       Roger Voss
freq           25514
Name: taster_name, dtype: object
reviews.taster_name.value_counts()

out:
Roger Voss 25514
Michael Schachner 15134
...
Fiona Adams 27
Christina Pickard 6
Name: taster_name, Length: 19, dtype: int64

각 이름이 몇 번 나왔는지 세어준다.

2. Maps

map()

review_points_mean = reviews.points.mean()
reviews.points.map(lambda p: p - review_points_mean)

이렇게 하면 편차를 구할 수 있다.

map()에 전달하는 함수는 Series의 형태로 반환이 된다.

apply()
만든 함수를 적용할 수 있게 해준다.

def remean_points(row):
    row.points = row.points - review_points_mean
    return row

reviews.apply(remean_points, axis='columns')

remean_points라는 함수를 만들었을 때, reviews에 적용할 수 있도록 해준다.

추가

reviews.country + " - " + reviews.region_1
out:
0            Italy - Etna
1                     NaN
               ...       
129969    France - Alsace
129970    France - Alsace
Length: 129971, dtype: object

이런 식으로 변형도 가능하다.

Grouping and Sorting

1. Groupwise analysis

reviews.groupby('points').points.count()
out:
points
80     397
81     692
      ... 
99      33
100     19

groupby()
주어진 와인에 동일한 포인트 값을 할당하는 리뷰 그룹을 생성함
각 그룹에 대해 points()열을 잡고 몇 번 나타나는지 계산을 한다.
groupby함수의 이해

reviews.groupby('winery').apply(lambda df: df.title.iloc[0])
out:

winery
1+1=3                          1+1=3 NV Rosé Sparkling (Cava)
10 Knots                 10 Knots 2010 Viognier (Paso Robles)
                                  ...                        
àMaurice    àMaurice 2013 Fred Estate Syrah (Walla Walla V...
Štoka                         Štoka 2009 Izbrani Teran (Kras)
Length: 16757, dtype: object
reviews.groupby(['country']).price.agg([len, min, max])
lenminmax
country
Argentina3800.04.0230.0
Armenia2.014.015.0
............
Ukraine14.06.013.0
Uruguay109.010.0130.0

agg()
동시에 다른 함수들을 표현할 수 있도록 해주는 함수이다.

0개의 댓글