Python-apply(lambda x: )的使用及说明
作者:程序媛小姑娘 发布时间:2022-05-11 04:31:29
Python-apply(lambda x: )使用
def instant_order_deal(plat, special_product, clearance_goods, new_product_instant,orders):
"""
:param plat: 要计算的平台
:param special_product: 特定库龄产品,其他平台的,amazon的在下面单独读取
:param clearance_goods: 清仓产品
:param new_product: 新品
:param orders: 订单
:return:
"""
# 退款订单处理
orders['订单总金额(包含客户运费、平台补贴)'] = orders.apply(lambda x: 0 if (x['订单类型'] == 'refund') else x['订单总金额(包含客户运费、平台补贴)'], axis=1)
"中间特定sku处理毛利"
# orders['毛利'] = orders.apply(lambda x: (x['平均采购价']* 0.4 + x['毛利']) if (x['产品代码'] == 'S4338867210')| (x['产品代码']=='S2130010010') else x['毛利'],axis=1)
orders['毛利'] = orders.apply(lambda x: (x['毛利'] + 5) if (x['产品代码'] == 'S1416028410') | (x['产品代码'] == 'S1416028440') | (x['产品代码'] == 'S1416028470') else x['毛利'], axis=1)
"""折价商品毛利计算 + 额温枪"""
depreciate = read_data().read_depreciate()
orders['毛利'] = orders.apply(lambda x: (x['平均采购价'] * 0.4 * x['数量'] + x['毛利']) if (x['产品代码'] in depreciate) and x['订单类型'] == 'sale' else x['毛利'],axis=1)
orders['平均采购价'] = orders.apply(lambda x: 0 if (x['订单类型'] == 'resend') else x['平均采购价'], axis=1)
# 中英仓处理
orders['仓库分类'] = orders.apply(lambda x: '中仓' if (x['发运仓库'] =='SH [上海奉贤仓]') | (x['发运仓库'] =='WZC [温州仓]') | (x['发运仓库'] =='SZC [深圳仓]') else '海外仓', axis=1)
# 处理新品
# if plat == 'ebay' or plat == 'shopee' or plat == 'amazon' :
newproduct = read_data().read_newproduct()
orders['仓库分类'] = orders.apply(lambda x: '新品' if (x['产品代码'] in newproduct) else x['仓库分类'], axis=1)
#处理海运产品
shipping = read_data().read_shipping()
orders['仓库分类'] =orders.apply(lambda x: '海运产品' if(x['产品代码'] in shipping and x['仓库分类'] != '海外仓') else x['仓库分类'],axis=1)
# 当月转清仓处理
orders['仓库分类'] = orders.apply(lambda x: '特定库龄'if isClearance(x['付款时间'], x['产品代码'], clearance_goods) != None else x['仓库分类'], axis=1)
# 特定库龄处理
orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if (x['发运仓库'] == 'GSE [古斯美东仓]' and x['平台']!='ebay') else x['仓库分类'], axis=1)
if plat == 'amazon':
# amazon的特定库龄需要单独读取
special_product_a = read_data().read_special_product(plat)
special_product_as = read_data().read_special_product('amazon特殊')
orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if (x['产品代码'] in special_product_as) else x['仓库分类'], axis=1)
orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if ((x['发运仓库'] + x['产品代码']) in special_product_a) else x['仓库分类'], axis=1)
else:
special_product = read_data().read_special_product('其他平台')
orders['仓库分类'] = orders.apply(lambda x: '特定库龄' if (x['产品代码'] in special_product) else x['仓库分类'], axis=1)
orders['仓库分类']=orders.apply(lambda x:'稳定期' if (x['仓库分类']=='中仓')| (x['仓库分类']=='海外仓' )else x['仓库分类'],axis=1 )
# 处理好仓库分类,接下来判断是否是开发新品
orders = pd.merge(orders, new_product_instant, on='产品代码', how='left')
orders['开发新品'] = orders['开发新品'].fillna('非开发新品')
# 然后处理货值
orders['货值'] = orders['数量'] * orders['平均采购价']
# orders = pd.merge(orders,mask_instant, on='产品代码', how='left')
# orders['口罩'] = orders['口罩'].fillna('非口罩')
return orders
python的lambda函数
Lambda 表达式
匿名函数的定义
在 Python 里有两类函数:
第一类:用 def 关键词定义的正规函数
第二类:用 lambda 关键词定义的匿名函数
Python 使用 lambda 关键词来创建匿名函数,而非def关键词,它没有函数名,其语法结构如下:
lambda argument_list: expression
lambda
- 定义匿名函数的关键词。argument_list
- 函数参数,它们可以是位置参数、默认参数、关键字参数,和正规函数里的参数类型一样。:
- 冒号,在函数参数和表达式中间要加个冒号。expression
- 只是一个表达式,输入函数参数,输出一些值。
注意:
expression 中没有 return 语句,因为 lambda 不需要它来返回,表达式本身结果就是返回值。
匿名函数拥有自己的命名空间,且不能访问自己参数列表之外或全局命名空间里的参数。
def sqr(x):
return x ** 2
print(sqr)
# <function sqr at 0x000000BABD3A4400>
y = [sqr(x) for x in range(10)]
print(y)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
lbd_sqr = lambda x: x ** 2
print(lbd_sqr)
# <function <lambda> at 0x000000BABB6AC1E0>
y = [lbd_sqr(x) for x in range(10)]
print(y)
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
sumary = lambda arg1, arg2: arg1 + arg2
print(sumary(10, 20)) # 30
func = lambda *args: sum(args)
print(func(1, 2, 3, 4, 5)) # 15
匿名函数的应用
函数式编程 是指代码中每一块都是不可变的,都由纯函数的形式组成。这里的纯函数,是指函数本身相互独立、互不影响,对于相同的输入,总会有相同的输出,没有任何副作用。
def f(x):
for i in range(0, len(x)):
x[i] += 10
return x
x = [1, 2, 3]
f(x)
print(x)
# [11, 12, 13]
def f(x):
y = []
for item in x:
y.append(item + 10)
return y
x = [1, 2, 3]
f(x)
print(x)
# [1, 2, 3]
匿名函数 常常应用于函数式编程的高阶函数 (high-order function)中,主要有两种形式:
参数是函数 (filter, map)
返回值是函数 (closure)
如,在 filter和map函数中的应用:
filter(function, iterable) 过滤序列,过滤掉不符合条件的元素,返回一个迭代器对象,如果要转换为列表,可以使用 list() 来转换。
odd = lambda x: x % 2 == 1
templist = filter(odd, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(templist)) # [1, 3, 5, 7, 9]
map(function, *iterables) 根据提供的函数对指定序列做映射。
m1 = map(lambda x: x ** 2, [1, 2, 3, 4, 5])
print(list(m1))
# [1, 4, 9, 16, 25]
m2 = map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
print(list(m2))
# [3, 7, 11, 15, 19]
除了 Python 这些内置函数,我们也可以自己定义高阶函数。
def apply_to_list(fun, some_list):
return fun(some_list)
lst = [1, 2, 3, 4, 5]
print(apply_to_list(sum, lst))
# 15
print(apply_to_list(len, lst))
# 5
print(apply_to_list(lambda x: sum(x) / len(x), lst))
# 3.0
来源:https://blog.csdn.net/TK_Phoenix/article/details/107710803


猜你喜欢
- 下载下来可是不会用啊,网上也找不到类似的方法,可能都没遇到过这样的问题,,经过一个晚上的研究demo及同事一起帮忙,终于研究出了如何使用,自
- 最近在学习Python的时候遇到一个知识点,在此记录下来可变参数会自动填充前面的同名默认参数比如下面这个函数def add_student(
- 一、下载MySQL登录MySQL官网下载MSI Installer:点击“Dnownload”点击“No thanks, just star
- 发现问题最近由于卸载Mysql时将很多相关依赖包都卸载了,重装mysql后启动django出现如下错误:django.core.except
- bootstrap中有javascript插件modal也就是对话框,加入拖拽功能,具体内容如下在使用modal时首选需要引用js<l
- 一、背景 今天闲着无事,写了一个小小的Python脚本程序,然后给同学炫耀的时候,发现每次都得拉着其他人过来看着自己的电脑屏幕,感觉不是很爽
- 在上一篇文章《深入理解 go Mutex》中, 我们已经对 go Mutex 的实现原理有了一个大致的了解,也知道了 Mutex 可以实现并
- 1. filter1.1 把一个序列中的空字符串删掉例如将[‘A’, ‘&
- 需求桌面临时文件较多时,直接删了不太放心,不删又显得很杂乱,故需要写一个脚本批量清理并备份这些鸡肋的文件。所以脚本需要具有以下功能1. 可以
- 年前帮manager 招GUI设计实习生 (PS. 这个实习生职位依然open,欢迎有兴趣的同学来投,邮箱jj.ying [at] hp.c
- 内容摘要:下面是虚机维护中,经常碰到的一些ASP程序中的数据库调用的错误,现收集整理如下:1.不能打开注册表关键字(8007000e);2.
- 概述从今天开始, 小白我将带领大家一起来补充一下 数据库的知识.数据控制语言数据控制语言 (Data Control Language) 是
- 一、运行时恐慌panicpanic是一种在运行时抛出来的异常。比如"index of range"。panic的详情:p
- 利用Python3来实现TCP协议,和UDP类似。UDP应用于及时通信,而TCP协议用来传送文件、命令等操作,因为这些数据不允许丢失,否则会
- 前言:用xtarbackup来同步数据,然后基于GTID来设置主从。一、用xtarbackup备份数据库1.1 优势使用xtarbackup
- django接口可以通过localhost或者127.0.0.1进行访问,但无法通过本机ip地址访问1. 修改django项目中的setti
- 1.ES6 解构[...arr, ...array]不改原数组值,生成新的数组。 2.遍历添加array.forEac
- SQL Server 2008我们也能从中体验到很多新的特性,但是对于SQL Server 2008安装,还是用图来说话比较好。本文将从SQ
- Vue.js 的核心是一个允许采用简洁的模板语法来声明式的将数据渲染进 DOM,也就是将模板中的文本数据写进DOM中,使用 {{
- MySQL修改字段的默认值和空值修改字段默认值修改:ALTER TABLE 表名 ALTER COLUMN 字段名 SET DEFAULT