1
import numpy as np
import pandas as pd

d = np.random.rand(10,3)
df = pd.DataFrame(d)
df

          0         1         2
0  0.740335  0.957937  0.611504
1  0.164772  0.469355  0.310799
2  0.666506  0.030054  0.986393
3  0.191827  0.554556  0.707453
4  0.278617  0.532423  0.095124
5  0.659526  0.413681  0.588372
6  0.285892  0.466231  0.268647
7  0.559664  0.274913  0.618610
8  0.572839  0.490969  0.194019
9  0.028767  0.332626  0.290148

# move the third row to top
df.iloc[:3] = df.iloc[[2,0,1]]
df
          0         1         2
0  0.666506  0.030054  0.986393
1  0.740335  0.957937  0.611504
2  0.164772  0.469355  0.310799
3  0.191827  0.554556  0.707453
4  0.278617  0.532423  0.095124
5  0.659526  0.413681  0.588372
6  0.285892  0.466231  0.268647
7  0.559664  0.274913  0.618610
8  0.572839  0.490969  0.194019
9  0.028767  0.332626  0.290148

The index is unchanged.

I tried df.index[:3] = df.index[[2,0,1]] (raise error) or df.index.values[:3] = df.index.values[[2,0,1]], there is no error raised but the index is still unchanged.

3
  • 1
    thanks for the comments. I edited with sample codes. Commented May 29, 2021 at 22:22
  • Are you expecting the index of the first 3 rows to also be 2,0,1 in this case? Commented May 29, 2021 at 22:33
  • Yes, I expect the index moves with the values Commented May 29, 2021 at 22:34

3 Answers 3

3

If you want the index to also reflect the updated row, you need to use df.reindex

In [25]: np.random.seed(5)
    ...: d = np.random.rand(5, 3)
    ...: df = pd.DataFrame(d)
    ...: df
Out[25]:
          0         1         2
0  0.221993  0.870732  0.206719
1  0.918611  0.488411  0.611744
2  0.765908  0.518418  0.296801
3  0.187721  0.080741  0.738440
4  0.441309  0.158310  0.879937

In [26]: df.reindex([2,0,1,*df.index[3:]])
Out[26]:
          0         1         2
2  0.765908  0.518418  0.296801
0  0.221993  0.870732  0.206719
1  0.918611  0.488411  0.611744
3  0.187721  0.080741  0.738440
4  0.441309  0.158310  0.879937
Sign up to request clarification or add additional context in comments.

Comments

1

How about this?

np.random.seed(42)
df = pd.DataFrame(np.random.rand(10,3))

enter image description here

df = df.iloc[[2,0,1]].append(df.iloc[3:])

enter image description here

1 Comment

you would like to arrange the first 3 rows and append the rest?
1

iloc will try to align the index.

For this reason, when the values are assigned back they will go right back to their initial location. Index 0 will align to index 0, index 1 will align to index 1, etc...

df.iloc[[2, 0, 1]]
          0         1         2
2  0.765908  0.518418  0.296801
0  0.221993  0.870732  0.206719
1  0.918611  0.488411  0.611744

(Note the index values)


To preserve the index values join the two DataFrame pieces together append or concat:

import numpy as np
import pandas as pd

np.random.seed(5)
d = np.random.rand(5, 3)
df = pd.DataFrame(d)

df = df.iloc[[2, 0, 1]].append(df.iloc[3:])

OR

df = pd.concat((df.iloc[[2, 0, 1]], df.iloc[3:]))

df Before:

          0         1         2
0  0.221993  0.870732  0.206719
1  0.918611  0.488411  0.611744
2  0.765908  0.518418  0.296801
3  0.187721  0.080741  0.738440
4  0.441309  0.158310  0.879937

df after:

          0         1         2
2  0.765908  0.518418  0.296801
0  0.221993  0.870732  0.206719
1  0.918611  0.488411  0.611744
3  0.187721  0.080741  0.738440
4  0.441309  0.158310  0.879937

(To match shown expected output)

To move without the indexes use values or to_numpy to prevent the aligning behaviour:

import numpy as np
import pandas as pd

np.random.seed(5)
d = np.random.rand(5, 3)
df = pd.DataFrame(d)

df.iloc[:3] = df.iloc[[2, 0, 1]].values

print(df)

df After:

          0         1         2
0  0.765908  0.518418  0.296801
1  0.221993  0.870732  0.206719
2  0.918611  0.488411  0.611744
3  0.187721  0.080741  0.738440
4  0.441309  0.158310  0.879937

4 Comments

How about the index? The index doesn't move with the row. and manually set df.index.values[:3] = df.index.values[[2,0,1]] doesn't work
You'll either need to join the pieces as I've noted in my edit, or create a new index based on the other answer. See the edits. @ddzzbbwwmm
For me, it's strange df.iloc[[2,0,1]] returns a dataframe with index also moved (as shown in your post), but the assignment only changes the values, not the index.
That's the point. iloc aligns indexes together. There are a ton of pandas operations where you want the order of your dataframe not to change when adding a new column (for example). You are specifically trying to get around one of the core data integrity features of pandas DataFrames.

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.