我有以下清单:
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)。我需要做以下几点:
中。
我尝试使用多个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)发布于 2021-02-10 11:22:49
首先,创建一个映射。您应该一直使用类似于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)https://stackoverflow.com/questions/66135794
复制相似问题