I have a dataframe as follows:
Edit: Based on comments and some shortcomings, I have edited the dataframe in question. The revised dataframe is as follows:
ID Static_Text Params
1 Today, {0} is quite Sunny. Tomorrow, {1} 1-10-2020
may be little {2}
1 Today, {0} is quite Sunny. Tomorrow, {1} 2-10-2020
may be little {2}
1 Today, {0} is quite Sunny. Tomorrow, {1} Cloudy
may be little {2}
2 Let's have a coffee break near {0}, if I Balcony
don't get any SO reply by {1}
2 Let's have a coffee break near {0}, if I 30
don't get any SO reply by {1} mins
What I want to achieve is :
ID Final Text
1 Today, 1-10-2020 is quite Sunny. Tomorrow, 2-10-2020
may be little Cloudy
2 Let's have a coffee break near Balcony, if I
don't get any SO reply by 30 mins
i.e. all params are inserted into the text.
Now there are two ways probably I can do this.
First, convert the dataframe like below,
ID Static Text Params
1 Today, {0} is quite Sunny. [1-10-2020,2-10-2020, Cloudy]
Tomorrow, {1} may be little {2}
And then I write a below function:
def update_text(x):
x['Static Text'] = x['Static Text'.format(x['Params'][0],x['Params'][1],x['Params'][2])]
rev_text = x['Static Text']
return rev_text
df['Final Text'] = df.apply(lambda x : update_text(x),axis=1)
In the above method, I am trying to get the params column values in a single row as list (as shown above). How can I achieve the same?
Edit: As we can see that for ID==2, I have 2 parameters. Hence update_text(x) function would not work.
Another method may be using df['Static Text'].str.replace(r'...', 'Params',n=1). However, I need to find the correct regex.
Either way I am not able to find a concrete solution. Can anybody help on this?
df.groupby(['ID','Static_Text'])['Params'].agg(list).reset_index(name='Params')?df.pivot(index='Static Text',columns='Params').reset_index(). What do you think?def update_text(x):part? Will that work? Also thelen(list(Params))can be changed. In that case how to manage theupdate_text(x)function?