Python-apply(lambda x: )的使用及說(shuō)明
Python-apply(lambda x: )使用
def instant_order_deal(plat, special_product, clearance_goods, new_product_instant,orders): """ :param plat: 要計(jì)算的平臺(tái) :param special_product: 特定庫(kù)齡產(chǎn)品,其他平臺(tái)的,amazon的在下面單獨(dú)讀取 :param clearance_goods: 清倉(cāng)產(chǎn)品 :param new_product: 新品 :param orders: 訂單 :return: """ # 退款訂單處理 orders['訂單總金額(包含客戶運(yùn)費(fèi)、平臺(tái)補(bǔ)貼)'] = orders.apply(lambda x: 0 if (x['訂單類型'] == 'refund') else x['訂單總金額(包含客戶運(yùn)費(fèi)、平臺(tái)補(bǔ)貼)'], axis=1) "中間特定sku處理毛利" # orders['毛利'] = orders.apply(lambda x: (x['平均采購(gòu)價(jià)']* 0.4 + x['毛利']) if (x['產(chǎn)品代碼'] == 'S4338867210')| (x['產(chǎn)品代碼']=='S2130010010') else x['毛利'],axis=1) orders['毛利'] = orders.apply(lambda x: (x['毛利'] + 5) if (x['產(chǎn)品代碼'] == 'S1416028410') | (x['產(chǎn)品代碼'] == 'S1416028440') | (x['產(chǎn)品代碼'] == 'S1416028470') else x['毛利'], axis=1) """折價(jià)商品毛利計(jì)算 + 額溫槍""" depreciate = read_data().read_depreciate() orders['毛利'] = orders.apply(lambda x: (x['平均采購(gòu)價(jià)'] * 0.4 * x['數(shù)量'] + x['毛利']) if (x['產(chǎn)品代碼'] in depreciate) and x['訂單類型'] == 'sale' else x['毛利'],axis=1) orders['平均采購(gòu)價(jià)'] = orders.apply(lambda x: 0 if (x['訂單類型'] == 'resend') else x['平均采購(gòu)價(jià)'], axis=1) # 中英倉(cāng)處理 orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '中倉(cāng)' if (x['發(fā)運(yùn)倉(cāng)庫(kù)'] =='SH [上海奉賢倉(cāng)]') | (x['發(fā)運(yùn)倉(cāng)庫(kù)'] =='WZC [溫州倉(cāng)]') | (x['發(fā)運(yùn)倉(cāng)庫(kù)'] =='SZC [深圳倉(cāng)]') else '海外倉(cāng)', axis=1) # 處理新品 # if plat == 'ebay' or plat == 'shopee' or plat == 'amazon' : newproduct = read_data().read_newproduct() orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '新品' if (x['產(chǎn)品代碼'] in newproduct) else x['倉(cāng)庫(kù)分類'], axis=1) #處理海運(yùn)產(chǎn)品 shipping = read_data().read_shipping() orders['倉(cāng)庫(kù)分類'] =orders.apply(lambda x: '海運(yùn)產(chǎn)品' if(x['產(chǎn)品代碼'] in shipping and x['倉(cāng)庫(kù)分類'] != '海外倉(cāng)') else x['倉(cāng)庫(kù)分類'],axis=1) # 當(dāng)月轉(zhuǎn)清倉(cāng)處理 orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '特定庫(kù)齡'if isClearance(x['付款時(shí)間'], x['產(chǎn)品代碼'], clearance_goods) != None else x['倉(cāng)庫(kù)分類'], axis=1) # 特定庫(kù)齡處理 orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '特定庫(kù)齡' if (x['發(fā)運(yùn)倉(cāng)庫(kù)'] == 'GSE [古斯美東倉(cāng)]' and x['平臺(tái)']!='ebay') else x['倉(cāng)庫(kù)分類'], axis=1) if plat == 'amazon': # amazon的特定庫(kù)齡需要單獨(dú)讀取 special_product_a = read_data().read_special_product(plat) special_product_as = read_data().read_special_product('amazon特殊') orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '特定庫(kù)齡' if (x['產(chǎn)品代碼'] in special_product_as) else x['倉(cāng)庫(kù)分類'], axis=1) orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '特定庫(kù)齡' if ((x['發(fā)運(yùn)倉(cāng)庫(kù)'] + x['產(chǎn)品代碼']) in special_product_a) else x['倉(cāng)庫(kù)分類'], axis=1) else: special_product = read_data().read_special_product('其他平臺(tái)') orders['倉(cāng)庫(kù)分類'] = orders.apply(lambda x: '特定庫(kù)齡' if (x['產(chǎn)品代碼'] in special_product) else x['倉(cāng)庫(kù)分類'], axis=1) orders['倉(cāng)庫(kù)分類']=orders.apply(lambda x:'穩(wěn)定期' if (x['倉(cāng)庫(kù)分類']=='中倉(cāng)')| (x['倉(cāng)庫(kù)分類']=='海外倉(cāng)' )else x['倉(cāng)庫(kù)分類'],axis=1 ) # 處理好倉(cāng)庫(kù)分類,接下來(lái)判斷是否是開(kāi)發(fā)新品 orders = pd.merge(orders, new_product_instant, on='產(chǎn)品代碼', how='left') orders['開(kāi)發(fā)新品'] = orders['開(kāi)發(fā)新品'].fillna('非開(kāi)發(fā)新品') # 然后處理貨值 orders['貨值'] = orders['數(shù)量'] * orders['平均采購(gòu)價(jià)'] # orders = pd.merge(orders,mask_instant, on='產(chǎn)品代碼', how='left') # orders['口罩'] = orders['口罩'].fillna('非口罩') return orders
python的lambda函數(shù)
Lambda 表達(dá)式
匿名函數(shù)的定義
在 Python 里有兩類函數(shù):
- 第一類:用 def 關(guān)鍵詞定義的正規(guī)函數(shù)
- 第二類:用 lambda 關(guān)鍵詞定義的匿名函數(shù)
Python 使用 lambda 關(guān)鍵詞來(lái)創(chuàng)建匿名函數(shù),而非def關(guān)鍵詞,它沒(méi)有函數(shù)名,其語(yǔ)法結(jié)構(gòu)如下:
lambda argument_list: expression
lambda
- 定義匿名函數(shù)的關(guān)鍵詞。argument_list
- 函數(shù)參數(shù),它們可以是位置參數(shù)、默認(rèn)參數(shù)、關(guān)鍵字參數(shù),和正規(guī)函數(shù)里的參數(shù)類型一樣。:
- 冒號(hào),在函數(shù)參數(shù)和表達(dá)式中間要加個(gè)冒號(hào)。expression
- 只是一個(gè)表達(dá)式,輸入函數(shù)參數(shù),輸出一些值。
注意:
- expression 中沒(méi)有 return 語(yǔ)句,因?yàn)?lambda 不需要它來(lái)返回,表達(dá)式本身結(jié)果就是返回值。
- 匿名函數(shù)擁有自己的命名空間,且不能訪問(wèn)自己參數(shù)列表之外或全局命名空間里的參數(shù)。
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
匿名函數(shù)的應(yīng)用
函數(shù)式編程 是指代碼中每一塊都是不可變的,都由純函數(shù)的形式組成。這里的純函數(shù),是指函數(shù)本身相互獨(dú)立、互不影響,對(duì)于相同的輸入,總會(huì)有相同的輸出,沒(méi)有任何副作用。
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]
匿名函數(shù) 常常應(yīng)用于函數(shù)式編程的高階函數(shù) (high-order function)中,主要有兩種形式:
- 參數(shù)是函數(shù) (filter, map)
- 返回值是函數(shù) (closure)
如,在 filter和map函數(shù)中的應(yīng)用:
filter(function, iterable) 過(guò)濾序列,過(guò)濾掉不符合條件的元素,返回一個(gè)迭代器對(duì)象,如果要轉(zhuǎn)換為列表,可以使用 list() 來(lái)轉(zhuǎn)換。
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) 根據(jù)提供的函數(shù)對(duì)指定序列做映射。
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 這些內(nèi)置函數(shù),我們也可以自己定義高階函數(shù)。
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
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python利用小波分析進(jìn)行特征提取的實(shí)例
今天小編就為大家分享一篇python利用小波分析進(jìn)行特征提取的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python生成器generator和yield關(guān)鍵字的使用
生成器是一種特殊的迭代器,可以通過(guò)列表推導(dǎo)式的修改或者使用yield關(guān)鍵字來(lái)創(chuàng)建,生成器函數(shù)能夠在迭代時(shí)動(dòng)態(tài)產(chǎn)生值,而不是一次性生成所有值,這有助于節(jié)省內(nèi)存,yield關(guān)鍵字使函數(shù)執(zhí)行暫停并保存當(dāng)前狀態(tài),下次調(diào)用時(shí)從停止處繼續(xù)執(zhí)行2024-09-09為什么選擇python編程語(yǔ)言入門黑客攻防 給你幾個(gè)理由!
為什么選擇python編程語(yǔ)言入門黑客攻防,小編今天給你幾個(gè)理由!Python語(yǔ)言的優(yōu)點(diǎn)、Python黑客攻擊優(yōu)點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02使用python實(shí)現(xiàn)CNN-GRU故障診斷的代碼示例
這篇文章主要給大家詳細(xì)介紹了如何使用python實(shí)現(xiàn)CNN-GRU故障診斷,文章中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07