2

I have a pandas data frame , that has some regression equations, with bias terms at the end of each equation. (+250 , -150, +450, +250 )

df:

    a           b
0   [TC100]+250 [TC200]-150
1   [FC100]+450 [FC200]+250

I would like to replace the bias terms [specifically , whatever comes after the last occurrence of the character ] in each equation] . The replacement string should be based on the corresponding column name. Desired output as below

output:

    a           b
0   [TC100]+a1  [TC200]+b1
1   [FC100]+a2  [FC200]+b2

I tried using rsplit , df.replace , Series.str.extract but no luck. I would appreciate very much any help .

2 Answers 2

1

Using split and just re-construct your str for each cell

s1=df.apply(lambda x : x.str.split(']',expand=True)[0])
df.astype(bool)
      a     b
0  True  True
1  True  True
s2=df.astype(bool)
s=s1+']+'+s2*s2.columns+(s2.T*(np.arange(len(df))+1).astype(str)).T
s
            a           b
0  [TC100]+a1  [TC200]+b1
1  [FC100]+a2  [FC200]+b2
Sign up to request clarification or add additional context in comments.

Comments

1

Or use apply in one-line (very long tho):

>>> df.apply(lambda x: x.str.split(']',expand=True)[0]+']+'+df.columns[df.isin([x[0]]).any()].item()+str(df[df.columns[df.isin([x[0]]).any()].item()].tolist().index(x[0])+1),axis=1)
            a           b
0  [TC100]+a1  [TC200]+a1
1  [FC100]+a2  [FC200]+a2
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.