user16795517
user16795517

Reputation:

String deduction

I have table with three columns: CG, TG and TG+ in the following format:

CG TG TG_plus
B13+ B13+ KNIT TOPS B13+ KNIT TOPS T-SHIRTS S-SL
B13+ G13+ WOVEN G13+ WOVEN TOPS SHIRTS L-SL

I need to obtain only KNIT TOPS from CG and T-SHIRTS S-SL from TG_plus. result:

CG TG TG_plus
B13+ KNIT TOPS T-SHIRTS S-SL
B13+ WOVEN TOPS SHIRTS L-SL

I haven't found any information of string deduction. Is it possible?

I did it by means of cycle


a = []
for i in range(len(df1)):
    b = df1['TG_plus'][i][len(df1['TG'][i]):]
    a.append(b)
df1['a'] = a

Are there any other ways to solve this task?

Upvotes: 0

Views: 83

Answers (1)

Corralien
Corralien

Reputation: 120409

If you use Pandas:

df1 = df.apply(lambda x: {'CG': x['CG'],
                          'TG': x['TG'][len(x['CG']):].strip(),
                          'TG_plus': x['TG_plus'][len(x['TG']):].strip()},
               axis=1, result_type='expand')

Output:

>>> df1
     CG         TG           TG_plus
0  B13+  KNIT TOPS     T-SHIRTS S-SL
1  G13+      WOVEN  TOPS SHIRTS L-SL

Upvotes: 2

Related Questions