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')
'Python' 카테고리의 다른 글
[Python] Pandas Course on Kaggle - 5 (0) | 2022.01.21 |
---|---|
[Python] Pandas Course on Kaggle - 4 (0) | 2022.01.20 |
[Python] Pandas Course on Kaggle - 2 (0) | 2022.01.17 |
[Python] Pandas Course on Kaggle - 1 (0) | 2022.01.15 |
[Python] finterstellar를 사용하여 Stochastic으로 주식 매매 시그널 만들기 (2) | 2021.12.14 |
댓글