Let's say I have this dataframe.
df = pd.DataFrame([['A-store',5,'B-store',4,'C-store',6], \
['B-store',3,'P-store',4,np.nan,np.nan], \
['N-store',20,np.nan,np.nan,'I-store',9], \
['L-store',8,'N-store',2,'A-store',5]],
columns=['store_1','time_1','store_2','time_2','store_3','time_3'])
store_1 time_1 store_2 time_2 store_3 time_3
0 A-store 5 B-store 4.0 C-store 6.0
1 B-store 3 P-store 4.0 NaN NaN
2 N-store 20 NaN NaN I-store 9.0
3 L-store 8 N-store 2.0 A-store 5.0
Ex: To get to the A-store it takes 5 minutes.
How can I sort the set of values (store, time) so that the leftmost set becomes the shortest and right most becomes the longest. I need to sort set of values over multiple columns. Also, it includes NaN.
Here is ideal output.
shorter <-----------------------------------> longer
store_1 time_1 store_2 time_2 store_3 time_3
0 B-store 4.0 A-store 5 C-store 6.0
1 B-store 3 P-store 4.0 NaN NaN
2 I-store 9.0 N-store 20 NaN NaN
3 N-store 2.0 A-store 5.0 L-store 8
I could probably pivot or stack, and sort by rows. But, I'm not sure how to do this.
If anyone have any good ideas or codes, let me know.
Thanks!