I have this dataframe as a list:
l = [["a",1,2,"","",""],["a",1,2,3,"",""], ["a","",2,"3","4",""],["a",1,"","",4,5]]
I would like to combine all those lines to obtain this final line :
Ideally, I would flatten the list of lists to fill the blank value where needed. What would be the pythonest way to do that ?
CodePudding user response:
Try:
import pandas as pd
import numpy as np
l = [["a",1,2,"","",""],["a",1,2,3,"",""], ["a","",2,"3","4",""],["a",1,"","",4,5]]
df = pd.DataFrame(l)
df.replace('',np.nan).ffill().tail(1)
df_out = df.replace('', np.nan).ffill().tail(1)
print(df_out)
Output:
0 1 2 3 4 5
3 a 1.0 2.0 3 4 5.0