Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

combine lines in dataframe with same identifiant

I have this dataframe as a list:

l = [["a",1,2,"","",""],["a",1,2,3,"",""], ["a","",2,"3","4",""],["a",1,"","",4,5]]

enter image description here

I would like to combine all those lines to obtain this final line :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Ideally, I would flatten the list of lists to fill the blank value where needed. What would be the pythonest way to do that ?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading