Skip to content Skip to sidebar Skip to footer

Swapping Two Rows (together With Index) Within The Same Pandas Dataframe

I am working on a flat resale price dataset (In case you are interested, I am using the Jan 2015 onward data). First, I group the data by using data.groupby('flat_type').sum(), T

Solution 1:

Use np.r_:

df = df.iloc[np.r_[0:len(df) - 2, -1, -2]]

Alternative:

df = df.loc[df.index[:-2].tolist() + df.index[-1:].tolist() + df.index[-2:-1].tolist()]

print(df)floor_area_sqmlease_commence_dateremaining_lease\flat_type1ROOM1085.0                6912519772ROOM40898.01788955623563ROOM1430010.14144832913443744ROOM3226160.46717732026040025ROOM2349460.0396632691555300MULTI-GENERATION4105.0                496771708EXECUTIVE943961.013059434495628resale_priceflat_type1ROOM6.938000e+062ROOM2.168112e+083ROOM6.628663e+094ROOM1.460442e+105ROOM1.043298e+10MULTI-GENERATION1.969189e+07EXECUTIVE4.101716e+09

Post a Comment for "Swapping Two Rows (together With Index) Within The Same Pandas Dataframe"