2

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(), Then the data becomes: enter image description here

My attempt to swap rows

Now, I try to swap the last two rows by using the answer from this post:

b,c = data_by_type.iloc[-2], data_by_type.iloc[-1]
temp = data_by_type.iloc[-2].copy()
data_by_type.iloc[-2] = c
data_by_type.iloc[-1] = b

enter image description here

The content for the last two rows changed, but the index for the last two rows remains the same.

So my question is: How to swap the entire row, including the index?

1 Answer 1

2

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_sqm  lease_commence_date  remaining_lease  \
flat_type                                                                
1 ROOM                    1085.0                69125             1977   
2 ROOM                   40898.0              1788955            62356   
3 ROOM                 1430010.1             41448329          1344374   
4 ROOM                 3226160.4             67177320          2604002   
5 ROOM                 2349460.0             39663269          1555300   
MULTI-GENERATION          4105.0                49677             1708   
EXECUTIVE               943961.0             13059434           495628   

                  resale_price  
flat_type                       
1 ROOM            6.938000e+06  
2 ROOM            2.168112e+08  
3 ROOM            6.628663e+09  
4 ROOM            1.460442e+10  
5 ROOM            1.043298e+10  
MULTI-GENERATION  1.969189e+07  
EXECUTIVE         4.101716e+09  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.