如何遍历数据帧中的行并交换两个相邻行,并对交换后创建的新数据帧执行一些操作?

时间:2019-02-18 02:53:53

标签: python dataframe pairwise interchange

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

执行时出现错误:

  

位置索引器超出范围

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

1 个答案:

答案 0 :(得分:0)

iloc用于:

print(df.iloc[[a for b in zip(df.index[::2][::-1],df.index[1::2][::-1]) for a in b]][::-1])
相关问题