1

I have a dataframe and I want to change the characters under the column Model. I want to use the codes solely and not the definition of the codes.

import pandas as pd

data = {
    'Model': ['Audi', 'Mercedes', 'Volkswagen'],
    'Code': ['["CFA4 - replace filters"]', '["C09 - reboot the engine"]', '["O06 - clean the exhaust pipe"]']
        }

df = pd.DataFrame(data)

Desired output

 Model       Code
 Audi        CFA4
 Mercedes    C09
 Volkswagen  O06
Andalusia
  • 43
  • 3

2 Answers2

0

using pd.eval and .explode with .str.split

df1 = df[['Model']].assign(
               Code=df['Code'].map(pd.eval).explode().str.split('-',expand=True)[0])


        Model  Code
0        Audi  CFA4
1    Mercedes   C09
2  Volkswagen   O06

or more simply using Regex and .str.extract

df1['CodeRegex'] = df['Code'].str.extract('\["(\w+)\s-')


        Model   Code CodeRegex
0        Audi  CFA4       CFA4
1    Mercedes   C09        C09
2  Volkswagen   O06        O06
Umar.H
  • 22,559
  • 7
  • 39
  • 74
0

Using only string functions, you can try:

df["Code"] = df["Code"].str[2:-2].str.split(" - ").str[0]

>>> df
        Model  Code
0        Audi  CFA4
1    Mercedes   C09
2  Volkswagen   O06
not_speshal
  • 22,093
  • 2
  • 15
  • 30