1

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?

7
  • Do you think df.groupby(['ID','Static_Text'])['Params'].agg(list).reset_index(name='Params') ? Commented Oct 1, 2020 at 10:41
  • @jezrael: let me try that out. I was thinking df.pivot(index='Static Text',columns='Params').reset_index(). What do you think? Commented Oct 1, 2020 at 10:55
  • Hmm, it not create column of lists. Commented Oct 1, 2020 at 10:57
  • True, but I get the multiple columns and then concatenate. However, there would be too much of work around. Can you please tell me the def update_text(x): part? Will that work? Also the len(list(Params)) can be changed. In that case how to manage the update_text(x) function? Commented Oct 1, 2020 at 11:02
  • I think yes, only necessary always 3 values in each list like in sample data. Commented Oct 1, 2020 at 11:03

1 Answer 1

0

Any luck on this? I think update_text() function needs to be modified as understood to perform dynamically manage the parameters in the string.

Sign up to request clarification or add additional context in comments.

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.