text: Q:
  
  How to remove a slow loop in Python and use Lambda or something else instead
  
  Replace a loop with lambda or something else to increase run speed
  I have a loop which works, but for my real data set it's going to be far too slow
  I basically have a huge text file, each line separated by \n characaters. 
  There is a distinctive message fingerprint at the beginning of each unique message, for the purposes of this let's say they begin with a #. I've put occurence of this # (Y) or not (N) in a separate column, called 'Beginning'
  I want to look for lines which don't begin with a # , and if the line below also doesn't begin with a # I want to concantenate the two. Ignore any desire to strip out \ns at the moment, I've got that covered.
  My loop works, but how can I do this using a lambda function or any other way to get a good speed up?
  Huge thanks in advance
  for i in range(2,(len(df)-1)):
      if ((df['Beginning'][i] == 'N') and (df['Beginning'][i+1] == 'N')):
          df['Message'][i] = df['Message'][i]  +  df['Message'][i+1]
          df['Message'][i+1] = ""
  
  An attempt at an edit to add an example:
  Message-begins-now 01:01:2018:12:15:28 \n
  bla bla text message \n
  details about location of issue \n
  specifics about somethign else \n
  Message-begins-now 01:01:2018:12:16:78 \n
  bla bla text message type 2 something xxxxxx \n
  Message-begins-now 01:01:2018:12:21:05 \n
  bla bla text message type 3 something xxxxxx \n
  location detail for this thing \n
  location detail for that thing \n
  price detail for me \n
  price detail for you \n
  lots \n
  more \n
  boring \n
  text \n
  Message-begins-now 01:01:2018:12:35:01 \n
  bla bla text message type 2 something xxxxxx \n
  So the above is 4 different messages, different lengths, and I want to concatenate the text so I have one row per message which contains all the info from beginning to end
  
  A:
  
  I think what you're looking for is df.shift()
  for example you can replace the iteration and if statement with something like this:
  df[(df['Beginning'] == df['Beginning'].shift(1)) & (df['Beginning'] == 'N')]
  
  or (what I would actually do)
  mask = (df['Beginning'] == df['Beginning'].shift(1)) & (df['Beginning'] == 'N')
  
  df.loc[mask, 'Message'] = df.loc[mask, 'Message'] + df.loc[mask, 'Message'].shift(1)  # you'd have to check that this is what you want, perhaps you need to shift the mask rather than the df, i'm not sure
  
  edit: oops, typos
  edit 2 - your question has changed, i'm less sure this will be helpful to you.
  
  