0

I have list of list:

my_list=[['06/20', '10:02:49am', '', '-125', 'Sunday'],
['', '09:57:51am', '+116', '-126', ''],
['', '09:56:02am', '', '-123', ''],
['', '09:53:36am', '', '-124',''],
['', '09:44:22am', '+114', '-123', ''],
['', '09:41:58am', '+115', '', ''],
['', '09:39:32am', '+112', '-121', ''],
['06/19', '10:02:49am', '+200', '-125', '']]

some nested items are empty, I want to fill them with the direct previous nested list item (except the last item), required result:

[['06/20', '10:02:49am', '+116', '-125', 'sunday'],
['06/19', '09:57:51am', '+116', '-126',''],
['06/19', '09:56:02am', '+114', '-123',''],
['06/19', '09:53:36am', '+114', '-124',''],
['06/19', '09:44:22am', '+114', '-123',''],
['06/19', '09:41:58am', '+115', '-121',''],
['06/19', '10:02:49am', '+112', '-121',''],
['06/19', '10:02:49am', '+200', '-125','']]

I used pandas to do the work then return the result back to list of list, but I need a result using pure python list comprehension or numpy because I need very fast execution and I need the result as list of list not dataframe. This is the pandas code which work very fine, but slow:

import pandas as pd
import numpy an np

df = pd.DataFrame(my_list)
df = df.replace('', np.nan)

df[0] = df[0].bfill()
df[1] = df[1].bfill()
df[2] = df[2].bfill()
df[3] = df[3].bfill()

#df
    0       1            2       3      4
0   06/20   10:02:49am  +116    -125    sunday
1   06/19   09:57:51am  +116    -126    
2   06/19   09:56:02am  +114    -123    
3   06/19   09:53:36am  +114    -124    
4   06/19   09:44:22am  +114    -123    
5   06/19   09:41:58am  +115    -121    
6   06/19   10:02:49am  +112    -121    
7   06/19   10:02:49am  +200    -125    

my_list = df.values.tolist() #work very fine, but I need an a faster alternative

0 Answers0