数据科学 100 道练习 61~70

数据科学 100 道练习 61~70#

import polars as pl
from helper.polars import load_100knocks_data
pl.Config.set_fmt_str_lengths(100)
df_customer, df_category, df_product, df_receipt, df_store, df_geocode = load_100knocks_data()

P-061#

对收据明细数据(df_receipt)的销售金额(amount),按顾客 ID(customer_id)合计,将合计金额取常用对数(底 10),与顾客 ID、销售金额合计一起显示 10 条。顾客 ID 以 "Z" 开头的是非会员,计算时排除在外。

(
df_receipt
.group_by('customer_id')
.agg(
    pl.col.amount.sum().alias('sum_amount'),
    (pl.col.amount.sum() + 0.5).log10().alias('log_amount')
)
.head()
)
shape: (5, 3)
customer_idsum_amountlog_amount
stri64f64
"CS023414000088"87983.944409
"CS016614000024"3062.48643
"CS015412000084"2202.343409
"CS002415000718"5382.731186
"CS019415000021"32943.51779

P-062#

对收据明细数据(df_receipt)的销售金额(amount),按顾客 ID(customer_id)合计,将合计金额取自然对数(底 e),与顾客 ID、销售金额合计一起显示 10 条。顾客 ID 以 "Z" 开头的是非会员,计算时排除在外。

(
df_receipt
.group_by('customer_id')
.agg(
    pl.col.amount.sum().alias('sum_amount'),
    (pl.col.amount.sum() + 0.5).log().alias('log_amount')
)
.head()
)
shape: (5, 3)
customer_idsum_amountlog_amount
stri64f64
"CS004412000219"24587.807307
"CS032415000225"33638.120737
"CS001514000168"16467.406407
"CS011513000188"7396.605974
"CS035214000006"42648.35808

P-063#

由商品数据(df_product)的单价(unit_price)和成本(unit_cost)计算各商品的利润额,显示 10 条结果。

(
df_product
.select(
    pl.col.product_cd,
    pl.col.unit_price,
    pl.col.unit_cost,
    (pl.col.unit_price - pl.col.unit_cost).alias('unit_profit')
)
.head()
)
shape: (5, 4)
product_cdunit_priceunit_costunit_profit
stri64i64i64
"P040101001"19814949
"P040101002"21816454
"P040101003"23017357
"P040101004"24818662
"P040101005"26820167

P-064#

由商品数据(df_product)的单价(unit_price)和成本(unit_cost)计算各商品利润率的整体平均值。注意单价和成本中存在缺失值。

(
df_product
.select(
    ((pl.col.unit_price - pl.col.unit_cost) / pl.col.unit_price).mean()
)
.item()
)
0.24911389885177

P-065#

对商品数据(df_product)的每个商品,求利润率达到 30% 的新单价。不足 1 日元的部分舍去。显示 10 条结果,并确认利润率在 30% 附近。注意单价(unit_price)和成本(unit_cost)中存在缺失值。

(
df_product
.select(
    pl.col.product_cd,
    pl.col.unit_price,
    pl.col.unit_cost,
    (pl.col.unit_cost / 0.7).floor().cast(pl.Int32).alias('new_price')
)
.with_columns(
    new_profit_rate=(pl.col.new_price - pl.col.unit_cost) / pl.col.new_price
)
.head(10)
)
shape: (10, 5)
product_cdunit_priceunit_costnew_pricenew_profit_rate
stri64i64i32f64
"P040101001"1981492120.29717
"P040101002"2181642340.299145
"P040101003"2301732470.299595
"P040101004"2481862650.298113
"P040101005"2682012870.299652
"P040101006"2982243200.3
"P040101007"3382543620.298343
"P040101008"4203154500.3
"P040101009"4983745340.299625
"P040101010"5804356210.299517

P-066#

对商品数据(df_product)的每个商品,求利润率达到 30% 的新单价。这次对不足 1 日元的部分做舍入(四舍五入或向偶数舍入均可)。显示 10 条结果,并确认利润率在 30% 附近。注意单价(unit_price)和成本(unit_cost)中存在缺失值。

(
df_product
.select(
    pl.col.product_cd,
    pl.col.unit_price,
    pl.col.unit_cost,
    (pl.col.unit_cost / 0.7)
        .round()
        .cast(pl.Int32)
        .alias('new_price')
)
.with_columns(
    new_profit_rate=(pl.col.new_price - pl.col.unit_cost) / pl.col.new_price
)
.head(10)
)
shape: (10, 5)
product_cdunit_priceunit_costnew_pricenew_profit_rate
stri64i64i32f64
"P040101001"1981492130.300469
"P040101002"2181642340.299145
"P040101003"2301732470.299595
"P040101004"2481862660.300752
"P040101005"2682012870.299652
"P040101006"2982243200.3
"P040101007"3382543630.300275
"P040101008"4203154500.3
"P040101009"4983745340.299625
"P040101010"5804356210.299517

P-067#

对商品数据(df_product)的每个商品,求利润率达到 30% 的新单价。这次对不足 1 日元的部分向上取整。显示 10 条结果,并确认利润率在 30% 附近。注意单价(unit_price)和成本(unit_cost)中存在缺失值。

(
df_product
.select(
    pl.col.product_cd,
    pl.col.unit_price,
    pl.col.unit_cost,
    (pl.col.unit_cost / 0.7)
        .ceil()
        .cast(pl.Int32)
        .alias('new_price')
)
.with_columns(
    new_profit_rate=(pl.col.new_price - pl.col.unit_cost) / pl.col.new_price
)
.head(10)
)
shape: (10, 5)
product_cdunit_priceunit_costnew_pricenew_profit_rate
stri64i64i32f64
"P040101001"1981492130.300469
"P040101002"2181642350.302128
"P040101003"2301732480.302419
"P040101004"2481862660.300752
"P040101005"2682012880.302083
"P040101006"2982243200.3
"P040101007"3382543630.300275
"P040101008"4203154500.3
"P040101009"4983745350.300935
"P040101010"5804356220.300643

P-068#

对商品数据(df_product)的每个商品,求消费税率为 10% 的含税金额。不足 1 日元的零头舍去,显示 10 条结果。注意单价(unit_price)中存在缺失值。

(
df_product
.select(
    pl.col.product_cd,
    pl.col.unit_price,
    (pl.col.unit_price * 1.1)
        .floor()
        .cast(pl.Int32)
        .alias('tex_price')
)
.head(10)
)
shape: (10, 3)
product_cdunit_pricetex_price
stri64i32
"P040101001"198217
"P040101002"218239
"P040101003"230253
"P040101004"248272
"P040101005"268294
"P040101006"298327
"P040101007"338371
"P040101008"420462
"P040101009"498547
"P040101010"580638

P-069#

连接收据明细数据(df_receipt)与商品数据(df_product),按顾客计算全部商品的销售金额合计,以及大类分类代码(category_major_cd)为 "07"(瓶装罐头)的销售金额合计,再求两者的比率。抽取对象仅限在大类分类代码 "07"(瓶装罐头)有销售实绩的顾客,显示 10 条结果。

(
df_receipt
.join(df_product, on='product_cd', how='left')
.group_by('customer_id', maintain_order=True)
.agg(
    sum_all=pl.col.amount.sum(),
    sum_07=pl.col.amount
        .filter(pl.col.category_major_cd == '07')
        .sum()
)
.with_columns(
    sales_rate=pl.col.sum_07 / pl.col.sum_all
)
.head(10)
)
shape: (10, 4)
customer_idsum_allsum_07sales_rate
stri64i64f64
"CS006214000001"736447130.640005
"CS008415000097"189513370.705541
"CS028414000014"622237010.594825
"ZZ000000000000"1239500369430090.560146
"CS025415000050"573635360.616457
"CS003515000195"541245810.846452
"CS024514000042"53300.0
"CS040415000178"614948020.78094
"CS027514000015"278814750.529053
"CS025415000134"490234300.699714

P-070#

对收据明细数据(df_receipt)的销售日期(sales_ymd),计算自顾客数据(df_customer)的会员申请日期(application_date)起经过的天数,与顾客 ID(customer_id)、销售日期、会员申请日期一起显示 10 条。注意 sales_ymd 以数值、application_date 以字符串保存。

(
df_receipt
.select('customer_id', 'sales_ymd')
.unique()
.join(df_customer, on='customer_id')
.select(
    pl.col.customer_id,
    pl.col.sales_ymd.cast(str).str.strptime(pl.Date, '%Y%m%d'),
    pl.col.application_date.cast(str).str.strptime(pl.Date, '%Y%m%d')
)
.with_columns(
    elapsed_days=(pl.col.sales_ymd - pl.col.application_date).dt.total_days()
)
.head()
)
shape: (5, 4)
customer_idsales_ymdapplication_dateelapsed_days
strdatedatei64
"CS044512000014"2018-11-012017-02-02637
"CS040415000066"2017-03-052015-07-09605
"CS003413000163"2018-02-232015-03-051086
"CS014513000201"2018-11-162015-05-291267
"CS037414000122"2019-05-272015-05-131475