2

I have a df that looks like this:

df
    time  score  
  83623      4  
  83624      3    
  83625      3   
  83629      2    
  83633      1    

I want to explode df.time so that the single digit increments by 1, and then the df.score value is duplicated for each added row. See example below:

    time  score  
  83623      4  
  83624      3    
  83625      3   
  83626      3
  83627      3
  83628      3
  83629      2
  83630      2
  83631      2
  83632      2 
  83633      1    
connor449
  • 1,549
  • 2
  • 18
  • 49
  • Does this answer your question? [How to unnest (explode) a column in a pandas DataFrame?](https://stackoverflow.com/questions/53218931/how-to-unnest-explode-a-column-in-a-pandas-dataframe) – msanford Aug 03 '20 at 17:29
  • I was looking at that post, but this post has rows in a list. I could convert my df.time values to list to reflect the range between rows), but I was looking for a more efficient way. – connor449 Aug 03 '20 at 17:32

1 Answers1

1

From your sample, I assume df.time is integer. You may try this way

df_final = df.set_index('time').reindex(range(df.time.min(), df.time.max()+1), 
                                        method='pad').reset_index()

Out[89]:
     time  score
0   83623      4
1   83624      3
2   83625      3
3   83626      3
4   83627      3
5   83628      3
6   83629      2
7   83630      2
8   83631      2
9   83632      2
10  83633      1
Andy L.
  • 24,909
  • 4
  • 17
  • 29