I have a following code which suppose to create a new column "TEMP1" which is a column "TEMP" with some replacements - replace #1 to a value of column PARM1:
import pandas as pd
d = {'col1': [1, 2], 'TEMP': ['kk(#1,#2)', 'kk(#1,#2)'], 'PARM1':['VAR1','VAR2'], 'PARM2':['VAR3','VAR1']}
df = pd.DataFrame(data=d)
df['TEMP1']=df['TEMP'].to_string(index=False).replace('#1',df['PARM1'].to_string(index=False))
print(df)
what i get is:
col1 TEMP PARM1 PARM2 TEMP1
0 1 kk(#1,#2) VAR1 VAR3 kk(VAR1\nVAR2,#2)\nkk(VAR1\nVAR2,#2)
1 2 kk(#1,#2) VAR2 VAR1 kk(VAR1\nVAR2,#2)\nkk(VAR1\nVAR2,#2)
so, instead of using only value of PARM1 in the same row, it replaces with all values of PARM1 separated by a new line symbol what i need is just
kk(VAR1,#2)
kk(VAR2,#2)
If your table format is pretty stable you could do something like this
d = {'col1': [1, 2], 'TEMP': ['kk(#1,#2)', 'kk(#1,#2)'], 'PARM1':['VAR1','VAR2'], 'PARM2':['VAR3','VAR1']}
df = pd.DataFrame(data=d)
df['TEMP1'] = df['TEMP']
for i in range(0, len(df)):
df.at[i, 'TEMP1'] = df.iloc[i]['TEMP1'].replace('#1', df.iloc[i]['PARM1'])
df
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.