其他分享
首页 > 其他分享> > Pandas笔记(二)

Pandas笔记(二)

作者:互联网

本文介绍常用Pandas列(Series)数据特征提取方法

我们以一组酒的数据为例,将数据保存到reviews,然后用heads()预览一下:

import pandas as pd
pd.set_option("display.max_rows", 5)
reviews = pd.read_csv("../input/wine-reviews/winemag-data-130k-v2.csv", index_col=0)

reviews.head()

后面列出一些针对Series的方法:

mean_points = reviews.points.means()
median_points = reviews.points.median()
countries = reviews.country.unique()

这里返回一个list

reviews_per_country = reviews.country.value_counts()

注意输出格式:
US 54504
France 22093
...
China 1
Egypt 1
Name: country, Length: 43, dtype: int64

bargain_idx = (reviews.points / reviews.price).idxmax()
bargain_wine = reviews.loc[bargain_idx, 'title']
price_mean = reviews.price.mean()
centered_price = reviews.price.map(lambda p: p - price_mean)

n_trop = reviews.description.map(lambda desc: "tropical" in desc).sum()
n_fruity = reviews.description.map(lambda desc: "fruity" in desc).sum()
descriptor_counts = pd.Series([n_trop, n_fruity], index=['tropical', 'fruity'])
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')

标签:country,price,笔记,reviews,points,Series,Pandas,row
来源: https://www.cnblogs.com/Asp1rant/p/15863986.html