title: What's the most Pythonic/efficient way to conditionally split lengthy rows?
body: <p>I have a data frame with a column <code>Results</code> which can get pretty lengthy. Since the data frame is ending up in an Excel report, this is problematic as Excel will only make a row so tall before it just simply doesn't display all data. Instead, what I'd like to do is split rows with results of a certain length into multiple rows. </p>
  
  <p>I wrote some code on a small scale data frame to split the results into chunks of 2. I haven't worked out how to put each chunk in a new row. Additionally, I'm not sure this will be the most efficient method when my data frame goes from 6 rows to 35k+. What is the most efficient/Pythonic way to achieve what I want? </p>
  
  <p>Original data frame</p>
  
  <pre><code>                        Result        Date
  0                    [SUCCESS]  10/10/2019
  1                    [SUCCESS]  10/09/2019
  2                    [FAILURE]  10/08/2019
  3  [Pending, Pending, SUCCESS]  10/07/2019
  4                    [FAILURE]  10/06/2019
  5           [Pending, SUCCESS]  10/05/2019
  </code></pre>
  
  <p>Goal output</p>
  
  <pre><code>               Result        Date
  0           [SUCCESS]  10/10/2019
  1           [SUCCESS]  10/09/2019
  2           [FAILURE]  10/08/2019
  3  [Pending, Pending]  10/07/2019
  4           [SUCCESS]  10/07/2019
  5           [FAILURE]  10/06/2019
  6  [Pending, SUCCESS]  10/05/2019
  </code></pre>
  
  <p>Code </p>
  
  <pre><code>import pandas as pd
  import numpy as np
  
  data = {'Result': [['SUCCESS'], ['SUCCESS'], ['FAILURE'], ['Pending', 'Pending', 'SUCCESS'], ['FAILURE'], ['Pending', 'SUCCESS']], 'Date': ['10/10/2019', '10/09/2019', '10/08/2019', '10/07/2019', '10/06/2019', '10/05/2019']}
  
  df = pd.DataFrame(data)
  df['Length of Results'] = df['Result'].str.len()
  
  def chunks(l, n):
      for i in range(0, len(l), n):
          yield l[i:i + n]
  
  for i in range(len(df)):
      if df['Length of Results'][i] &gt; 2:
          df['Result'][i] = list(chunks(df['Result'][i], 2))
      else:
          pass
  
  df['Chunks'] = 1
  for i in range(len(df)):
      if df['Length of Results'][i] &gt; 2:
          df['Chunks'][i] = len(df['Result'][i])
      else:
          pass
  
  df = df.loc[np.repeat(df.index.values, df.Chunks)]
  df = df.reset_index(drop=True)
  </code></pre>
  
  <p>Code currently produces</p>
  
  <pre><code>                            Result        Date  Length of Results  Chunks
  0                        [SUCCESS]  10/10/2019                  1       1
  1                        [SUCCESS]  10/09/2019                  1       1
  2                        [FAILURE]  10/08/2019                  1       1
  3  [[Pending, Pending], [SUCCESS]]  10/07/2019                  3       2
  4  [[Pending, Pending], [SUCCESS]]  10/07/2019                  3       2
  5                        [FAILURE]  10/06/2019                  1       1
  6               [Pending, SUCCESS]  10/05/2019                  2       1
  </code></pre>
  
  <p>df.to_dict()</p>
  
  <pre><code>{'Result': {0: ['SUCCESS'], 1: ['SUCCESS'], 2: ['FAILURE'], 3: [['Pending', 'Pending'], ['SUCCESS']], 4: [['Pending', 'Pending'], ['SUCCESS']], 5: ['FAILURE'], 6: ['Pending', 'SUCCESS']}, 'Date': {0: '10/10/2019', 1: '10/09/2019', 2: '10/08/2019', 3: '10/07/2019', 4: '10/07/2019', 5: '10/06/2019', 6: '10/05/2019'}, 'Length of Results': {0: 1, 1: 1, 2: 1, 3: 3, 4: 3, 5: 1, 6: 2}, 'Chunks': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2, 5: 1, 6: 1}}
  </code></pre>