There is a table:

c1 0 A 1 B 2 C 3 D 4 E 5 F 

The indices of this table need to be rewritten in new pairs:

  c1 c2 0 0 1 1 2 3 2 4 5 
  • Do you have an even number of lines guaranteed? - MaxU
  • No, but the method will come down for an even count. - Vlad
  • and what to do with the extra line in the case of an odd number of lines? - MaxU
  • write it together with '', if possible - Vlad

1 answer 1

 from itertools import zip_longest pd.DataFrame(list(zip_longest(d.index[::2], d.index[1::2])), columns=['c1','c2']) 

Example for an even number of lines:

 In [116]: d Out[116]: c1 0 A 1 B 2 C 3 D 4 E 5 F In [117]: from itertools import zip_longest In [118]: pd.DataFrame(list(zip_longest(d.index[::2], d.index[1::2])), columns=['c1','c2']) Out[118]: c1 c2 0 0 1 1 2 3 2 4 5 

Example for an odd number of lines:

 In [119]: d2 Out[119]: c1 0 A 1 B 2 C 3 D 4 E In [120]: pd.DataFrame(list(zip_longest(d2.index[::2], d2.index[1::2])), columns=['c1','c2']) Out[120]: c1 c2 0 0 1.0 1 2 3.0 2 4 NaN 
  • Thanks, helped out) - Vlad
  • @Vlad please! :) - MaxU