2

I have a dataframe :

start  end
1      10
26     50
6      15
1      5
11     25

I expect following dataframe :

start  end
1      10
11     25
26     50
1      5
6      15

here sort order is noting but end of nth row must be start+1 of n+1th row.If not found, search for other starts where start is one.

can anyone suggest what combination of sort and group by can I use to convert above dataframe in required format?

1 Answers1

0

You could transform the df to a list and then do:

l=[1,10,26,50,6,15,1,5,11,25]
result=[]
for x in range(int(len(l)/2)):
    result.append(sorted([l[2*x],l[2*x+1]])[1])
    result.append(sorted([l[2*x],l[2*x+1]])[0])

This will give you result:

[1, 10, 26, 50, 6, 15, 1, 5, 11, 25]

To transform the original df to list you can do:

startcollist=df['start'].values.tolist()

endcollist=df['end'].values.tolist()

l=[]
for index, each in enumerate(originaldf):
    l.append(each)
    l.append(endcollist[index])

You can then transform result back to a dataframe:

df=pd.DataFrame({'start':result[1::2], 'end':result[0::2]})

Giving the result:

    end start
0   10  1
1   50  26
2   15  6
3   5   1
4   25  11

The expression result[1::2] gives every odd element of result, result[0::2] gives every even element. For explanation, see here: https://stackoverflow.com/a/12433705/8565438

zabop
  • 6,750
  • 3
  • 39
  • 84