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.
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?
You can rework the data after concatenation with numpy:
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