简体   繁体   English

大熊猫中的字符串替换

[英]string replace in pandas

I have a pandas data frame , that has some regression equations, with bias terms at the end of each equation.我有一个 Pandas 数据框,它有一些回归方程,每个方程末尾都有偏差项。 (+250 , -150, +450, +250 ) (+250, -150, +450, +250 )

df: 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.我尝试使用rsplitdf.replaceSeries.str.extract但没有运气。 I would appreciate very much any help .我将不胜感激任何帮助。

Using split and just re-construct your str for each cell使用split并为每个单元格重新构建你的 str

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

Or use apply in one-line (very long tho):或者在一行中使用apply (很长):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM