简体   繁体   中英

How do I iterate over the rows in dataframe and swap two adjacent rows and perform some operations on the new dataframe created after the swap?

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`

我在Spyder中的代码 完整的代码以及每次交换后要执行的操作

使用iloc与:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM