首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -在列/多个lambda语句中检查列表成员资格和存储父组名称?

Python -在列/多个lambda语句中检查列表成员资格和存储父组名称?
EN

Stack Overflow用户
提问于 2021-02-10 11:14:55
回答 1查看 22关注 0票数 0

我有以下清单:

代码语言:javascript
复制
automotive = ["Audio Amp", "Digital Radio", "FM AM Auto", "XM Radio"]
home = ["Codec", "FM AM Tuner", "FM Tuner","FM TX Tuner","Satellite Receiver","TV Demodulator","TV Receiver","TV Tuner"]
b8_mcu = ["Automotive","Broad Based","EFM8","Low Power","Prec Mxd-Signal","USB"]
iot15_4 = ["15.4 IC","15.4 Modules","End Products"]
b32_mcu = ["EFM32 Classic","EFM32 Gemstone","EFM32 Predator","Precision 32"]
bluetooth = ["Bluetooth IC","Bluetooth Modules","BT Classic & SR Modules","BT Smart Modules"]
proprietary = ["8b Wireless MCU","32b Wireless MCU","Transceiver"]
sensors = ["Analog","Hall Position Sensor","IRDA","Optical Sensor","RHT Sensor"]
touch = ["Multi-Touch"]
wifi = ["Wi-Fi Classic Modules","Wi-Fi IC","Wi-Fi Modules","Zentri Classic"]
zwave = ["Z-Wave IC","Z-Wave Modules"]
access = ["ADSL","ASIC","Modem","ProSLIC"]
power = ["Isolation","PoE"]
timing = ["Buffers","Clock","MEMS","OSC","PHY","Sync Modules","Synth"]

此外,我还有一个(df1_1)。我需要做以下几点:

  1. 检查my dataframe (df1_1"Product“)中的列是否具有来自
  2. 上的任何一个列表的值,创建一个新列到dataframe (df1_1"GMPL"),该列将将值所属的列表名称存储到.

中。

我尝试使用多个lambda语句,但每一行都覆盖前面的语句:

代码语言:javascript
复制
df1_1["GMPL"] = 0


df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'automotive' if x in automotive else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'home' if x in home else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'b8_mcu' if x in b8_mcu else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'iot15_4' if x in iot15_4 else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'b32_mcu' if x in b32_mcu else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'bluetooth' if x in bluetooth else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'proprietary' if x in proprietary else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'sensors' if x in sensors else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'touch' if x in touch else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'wifi' if x in wifi else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'zwave' if x in zwave else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'access' if x in access else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'power' if x in power else x)
df1_1['GMPL'] = df1_1["Product Line"].apply(lambda x : 'timing' if x in timing else x)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-10 11:22:49

首先,创建一个映射。您应该一直使用类似于dict的东西,变量的名称不应该包含数据。变量名是用于读取源代码的人,而不是计算机。如果需要将字符串映射到其他字符串,请使用dict

代码语言:javascript
复制
mapping = dict(
  automotive = ["Audio Amp", "Digital Radio", "FM AM Auto", "XM Radio"],
  home = ["Codec", "FM AM Tuner", "FM Tuner","FM TX Tuner","Satellite Receiver","TV Demodulator","TV Receiver","TV Tuner"],
  b8_mcu = ["Automotive","Broad Based","EFM8","Low Power","Prec Mxd-Signal","USB"],
  iot15_4 = ["15.4 IC","15.4 Modules","End Products"],
  b32_mcu = ["EFM32 Classic","EFM32 Gemstone","EFM32 Predator","Precision 32"],
  bluetooth = ["Bluetooth IC","Bluetooth Modules","BT Classic & SR Modules","BT Smart Modules"],
  proprietary = ["8b Wireless MCU","32b Wireless MCU","Transceiver"],
  sensors = ["Analog","Hall Position Sensor","IRDA","Optical Sensor","RHT Sensor"],
  touch = ["Multi-Touch"],
  wifi = ["Wi-Fi Classic Modules","Wi-Fi IC","Wi-Fi Modules","Zentri Classic"],
  zwave = ["Z-Wave IC","Z-Wave Modules"],
  access = ["ADSL","ASIC","Modem","ProSLIC"],
  power = ["Isolation","PoE"],
  timing = ["Buffers","Clock","MEMS","OSC","PHY","Sync Modules","Synth"],
)

实际上,您需要反向映射:

代码语言:javascript
复制
mapping = {v:k for k,vs in mapping.items() for v in vs}

然后,只需使用:

代码语言:javascript
复制
df1_1["GMPL"] = df1_1["Product Line"].map(mapping)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66135794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档