본문 바로가기
Python

[Python] Pandas Course on Kaggle - 3

by llHoYall 2022. 1. 19.

This is the solution of pandas course (Summary Functions and Maps) on Kaggle site.

1. median() Function

median_points = reviews.points.median()
# or
median_points = reviews["points"].median()

2. unique() Function

countries = reviews.country.unique()
# or
countries = reviews["country"].unique()

3. value_counts() Function

reviews_per_country = reviews.country.value_counts()
# or
reviews_per_country = reviews["country"].value_counts()

4. mean() Function

centered_price = reviews.price - reviews.price.mean()
# or
centered_price = reviews["price"] - reviews["price"].mean()

5. idxmax() Function

bargain_wine = reviews.loc[(reviews.points / reviews.price).idxmax(), 'title']
# or
bargain_wine = reviews.loc[(reviews["points"] / reviews["price"]).idxmax(), 'title']

6.  Map

descriptor_counts = pd.Series(
    [
        reviews.description.map(lambda desc: "tropical" in desc).sum(), 
        reviews.description.map(lambda desc: "fruity" in desc).sum()
    ], 
    index=['tropical', 'fruity']
)
# or
descriptor_counts = pd.Series(
    [
        reviews["description"].map(lambda desc: "tropical" in desc).sum(), 
        reviews["description"].map(lambda desc: "fruity" in desc).sum()
    ], 
    index=['tropical', 'fruity']
)

7.  apply() Function

def stars(row):
    if row.country == 'Canada':
        return 3
    elif row.points >= 95:
        return 3
    elif row.points >= 85:
        return 2
    else:
        return 1

star_ratings = reviews.apply(stars, axis='columns')

댓글