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 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 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 Source: https://ru.stackoverflow.com/questions/722284/
All Articles