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
猜你喜欢
- 1、创建表的同时创建主键约束(1)无命名create table student ( studentid int primary key n
- 一、绘制线性图形执行如下代码import matplotlib.pyplot as pltdataX=[1,2,3,4]dataY=[2,4
- 1、基于字典的创建规划问题上篇中介绍了使用 LpVariable 对逐一定义每个决策变量,设定名称、类型和上下界,类似地对约束条件也需要逐一
- JavaScript组件打包模式js组件通常带着css image ,但这样使用起来可能会有些小麻烦,为了让组件足够的solo,有了把css
- Celery是Python开发分布式任务列队的处理库。可以异步分布式地异步处理任务,也可定时执行任务等等。通常我们可以使用celery在Dj
- 最近在写的一个django小项目需要实现用户上传图片的功能,使用到了七牛云存储,特此记录下来。这里我使用的七牛python SDK 版本是7
- 阅读上一篇:一个完美网站的101项指标.第三部分.易用性 设计体现了一个网站的艺术素养,然而并不是说您应当过分设计,设计风格应结合您的行业,
- <% Function cutbadchar(str) badstr="不|文|明
- 需要写个js滑动展开折叠(收缩)的效果,搜索到无忧脚本的一篇贴子,稍加修改了下使其在FF也可应用,代码如下: <
- 概述本文主要介绍一种降维方法,PCA(Principal Component Analysis,主成分分析)。降维致力于解决三类问题。1.
- 本文实例为大家分享了Python实现学生信息管理系统的具体代码,供大家参考,具体内容如下要求描述:学生的信息包括:学号,姓名,年龄,性别,出
- python语言本身没有提供const,但实际开发中经常会遇到需要使用const的情形,由于语言本身没有这种支出,因此需要使用一些技巧来实现
- SQL Server 2005 和 2008 有几个关于高可用性的选项,如日志传输、副本和数据库镜像。所有这些技术都能够作为维护一个备用服务
- <?php/* Function Written by Nelson Neoh @3/2004. For th
- 代码如下: var params = new Enumerator(Request.QueryString); while (!params
- 本文实例讲述了python实现linux下使用xcopy的方法。分享给大家供大家参考。具体如下:这个python函数模仿windows下的x
- 如何查看cpu的核数代码: from multiprocessing import cpu_count print(&q
- 本文实例为大家分享了python os模块在系统管理中的应用代码,供大家参考,具体内容如下#临时文件import tempfile temp
- PHP 过滤器PHP 过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入。什么是 PHP 过滤器PHP 过滤器用于验证和过滤来自非安全
- 验证码制作#string模块自带数字、字母、特殊字符变量集合,不需要我们手写集合import stringimport randomimpo