У меня есть следующие списки:

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"]), в котором будет храниться имя списка, которому принадлежит значение.

Я пробовал использовать несколько лямбда-операторов, но каждая строка перезаписывает предыдущую:

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)
0
Alex Günsberg 10 Фев 2021 в 14:14

1 ответ

Лучший ответ

Сначала создайте сопоставление. Вы должны были использовать что-то вроде 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)
0
juanpa.arrivillaga 10 Фев 2021 в 11:22