0

Guys i have this excel data here as

enter image description here

and i want to convert it to something like this

enter image description here

FACTORYLINECODE is just VBR

SMP NO = STD(priority 1)

USAP NO = USA(priority 2)

OE NO. = OEM(priority 3)

ignore PG order

i achived this through power query. im new to pandas and python and would love to learn how would i achive this with pandas. i thougt of using split at OE NO. but stuck there

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

2

The most important part of your problem is to clean your data before melt:

d = {'V-Bright NO.': 'Factory Part', 'SMP NO.': 'STD', 'USAP NO.': 'USA', 'OE NO.': 'OEM'}
p = {'STD': 1, 'USA': 2, 'OEM': 3}
c = ['FactoryLinecode', 'Factory Part', 'Linecode', 'InterchangePart', 'Priority']

out = (df.rename(columns=d)[d.values()]
         .melt(['Factory Part'], var_name='Linecode', value_name='InterchangePart')
         .assign(InterchangePart=lambda x: x['InterchangePart'].str.split('\n'),
                 Priority=lambda x: x['Linecode'].map(p), FactoryLinecode='VBR')
         .explode('InterchangePart')[c])

Output:

FactoryLinecode Factory Part Linecode InterchangePart Priority
VBR VB-9400 STD UF499 1
VBR VB-9400 USA REPK504603 2
VBR VB-9400 OEM 2730126640 3
VBR VB-9400 OEM 0986221077 3
Corralien
  • 109,409
  • 8
  • 28
  • 52