2
for i, rows in df.iterrows():
    x, y = df.iloc[rows].copy(), df.iloc[rows+1].copy()
    df.iloc[rows], df.iloc[rows+1] = y, x
    break

I get error on execution:

positional indexers are out-of-bounds`

My code in Spyder Complete code with operations to be performed after each swap

1 Answers1

0

Use iloc with:

print(df.iloc[[a for b in zip(df.index[::2][::-1],df.index[1::2][::-1]) for a in b]][::-1])
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Thanks! it is working but it is swapping all the rows at once and giving the output. How can I swap one row at a time and perform some operations on it and then move on to the next swap? – Abdul Raheem Feb 19 '19 at 01:53
  • I have attached a snippet of my code with operations to be performed after each swap. Please, look into it and let me know how to rectify the errors. – Abdul Raheem Feb 19 '19 at 02:00