This is the solution of pandas course (Grouping and Sorting) on Kaggle site.
1. Group by Column
reviews_written = reviews.groupby('taster_twitter_handle').size()
# or
reviews_written = reviews.groupby('taster_twitter_handle')
.taster_twitter_handle
.count()
# or
reviews_written = reviews.groupby("taster_twitter_handle")[
"taster_twitter_handle"
].count()
2. Group by Column and Sort by Index
best_rating_per_price = reviews.groupby('price').points.max().sort_index()
# or
best_rating_per_price = reviews.groupby('price')['points'].max().sort_index()
3. Group by Min and Max
price_extremes = reviews.groupby('variety').price.agg([min, max])
# or
price_extremes = reviews.groupby('variety')["price"].agg([min, max])
4. Sort by Columns
sorted_varieties = price_extremes.sort_values(by=['min', 'max'], ascending=False)
5. Group by Column and Apply mean() Function
reviewer_mean_ratings = reviews.groupby('taster_name').points.mean()
# or
reviewer_mean_ratings = reviews.groupby('taster_name')["points"].mean()
6. Group by Columns and Sort by Value
country_variety_counts = reviews.groupby(['country', 'variety'])
.size()
.sort_values(ascending=False)
'Python' 카테고리의 다른 글
[Python] Pandas Course on Kaggle - 6 (0) | 2022.01.22 |
---|---|
[Python] Pandas Course on Kaggle - 5 (0) | 2022.01.21 |
[Python] Pandas Course on Kaggle - 3 (0) | 2022.01.19 |
[Python] Pandas Course on Kaggle - 2 (0) | 2022.01.17 |
[Python] Pandas Course on Kaggle - 1 (0) | 2022.01.15 |
댓글