pandas 3 的 pd.col
简单
22-pandas-only pandas 专属
assignpd-colexpression
给商品表加三列派生列。
本题的考点是 pandas 3 新增的列表达式写法:在 assign 里引用列时,不用 lambda,也不重复写表名。判题只看结果,但请用这种写法完成。
margin = price - cost
margin_rate = margin / price(用上一步算出的 margin),不做舍入
is_premium:price >= 100 时为 true,否则为 false
null 的处理:price 或 cost 为 null 时,margin 和 margin_rate 为 null;price 为 null 时 is_premium 为 false。
输入
products(product_id, name, category, price, cost, launched_on)
输出
列为 product_id, name, margin, margin_rate, is_premium,每个商品一行,行顺序不限。
输入表 (case base,共 2 个 case:base、nulls)
期望输出 (case base,行顺序不限)
| # | product_idInt64 | nameString | marginFloat64 | margin_rateFloat64 | is_premiumBoolean |
| 1 |
1 |
Wireless Mouse |
8.23 |
0.3292 |
False |
| 2 |
2 |
USB-C Hub |
15.73 |
0.3152304609 |
False |
| 3 |
3 |
Bluetooth Speaker |
27.53 |
0.3093258427 |
False |
| 4 |
4 |
Mechanical Keyboard |
93.52 |
0.4699497487 |
True |
| 5 |
5 |
4K Monitor |
217.62 |
0.43524 |
True |
| 6 |
6 |
Noise-cancel Headphones |
87.43 |
0.2924080268 |
True |
| 7 |
7 |
Python Cookbook |
28.92 |
0.4901694915 |
False |
| 8 |
8 |
SQL Pocket Guide |
17.76 |
0.445112782 |
False |
| 9 |
9 |
数据分析实战 |
38.17 |
0.483164557 |
False |
| 10 |
10 |
Clean Code |
21.52 |
0.4304 |
False |
| 11 |
11 |
统计学习方法 |
28.78 |
0.2907070707 |
False |
| 12 |
12 |
Desk Lamp |
20.01 |
0.4446666667 |
False |
| 13 |
13 |
Ceramic Mug |
8.56 |
0.4301507538 |
False |
| 14 |
14 |
Throw Pillow |
15.77 |
0.4505714286 |
False |
| 15 |
15 |
Wall Clock |
51.02 |
0.5153535354 |
False |
| 16 |
16 |
Storage Box |
14.84 |
0.4963210702 |
False |
| 17 |
17 |
Puzzle 1000pc |
69.47 |
0.5385271318 |
True |
| 18 |
18 |
Building Blocks |
88.81 |
0.446281407 |
True |
| 19 |
19 |
RC Car |
150.45 |
0.4190807799 |
True |
| 20 |
20 |
Plush Panda |
45.86 |
0.5152808989 |
False |
| 21 |
21 |
Yoga Mat |
49.3 |
0.493 |
True |
| 22 |
22 |
Dumbbell Set |
84.0 |
0.3373493976 |
True |
| 23 |
23 |
Running Shoes |
275.42 |
0.3940200286 |
True |
| 24 |
24 |
Jump Rope |
5.16 |
0.344 |
False |
| 25 |
25 |
Water Bottle |
17.59 |
0.4408521303 |
False |
| 26 |
26 |
Face Cream |
53.07 |
0.26535 |
True |
| 27 |
27 |
Lip Balm |
4.54 |
0.3632 |
False |
| 28 |
28 |
Shampoo |
16.67 |
0.3340681363 |
False |
| 29 |
29 |
Sunscreen |
63.26 |
0.3978616352 |
True |
| 30 |
30 |
Hand Lotion |
9.95 |
0.4522727273 |
False |
共 30 行
参考答案 · pandas 3 的 pd.col