Siva-Sg
Siva-Sg

Reputation: 2821

string replace in pandas

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 .

Upvotes: 2

Views: 104

Answers (2)

U13-Forward
U13-Forward

Reputation: 71560

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
>>> 

Upvotes: 1

BENY
BENY

Reputation: 323226

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

Upvotes: 1

Related Questions