Python - 检查列表成员身份并将父组名称存储在列/多个 lambda 语句中?

亚历克斯·冈斯伯格

我有以下列表:

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"]

另外我有一个 Pandas 数据框 (df1_1)。我需要执行以下操作:

  1. 检查我的数据框中的列 (df1_1["Product Line"]) 是否具有上述任何列表中的值
  2. 为数据框创建一个新列 (df1_1["GMPL"]),它将存储值所属的列表名称。

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

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)
胡安帕阿里维拉加

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

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"],
)

你实际上需要反向映射:

mapping = {v:k for k,vs in mapping.items() for v in vs}

然后,只需使用:

df1_1["GMPL"] = df1_1["Product Line"].map(mapping)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章