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

Align according to a specific column in table generated by pandas

I have some dataframes that are generated using pandas.

df1 = pd.DataFrame({"A": ['a','b','c','d']})
df2 = pd.DataFrame({"B": ['','c','a','']})
df3 = pd.DataFrame({"C": ['','','d','']})

df=pd.concat([df1,df2,df3],axis=1)
df

This is the result of the code.

enter image description here

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

I am looking for alignment of the columns in the right considering the row with the more elements, like here below. I have tried and searched for different strategies, but they did work. Some suggestions?

enter image description here

>Solution :

You can rework the data after concatenation with :

import numpy as np

df = pd.concat([df1,df2,df3],axis=1)

# convert the DataFrame to array
a = df.to_numpy()

# push the empty cells to the left and assign back
df[:] = a[np.arange(len(a))[:,None], np.argsort(a!='', axis=1)]

Output:

   A  B  C
0        a
1     b  c
2  c  a  d
3        d
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