0

I have the the below df build from a pivot of a larger df. In this table 'week' is the the index (dtype = object) and I need to show week 53 as the first row instead of the last

Can someone advice please? I tried reindex and custom sorting but can't find the way Thanks!

here is the table

sawabcn
  • 3
  • 2
  • Can you clarify if you just want to move the row 53 to the top and keep current order or re-order the frame in descending order? – rosaqq Jun 23 '21 at 10:54
  • 1
    Possible duplicate: https://stackoverflow.com/questions/38980507/moving-desired-row-to-the-top-of-pandas-data-frame – rosaqq Jun 23 '21 at 11:14

2 Answers2

0

One way of doing this would be:

import pandas as pd
df = pd.DataFrame(range(10))
new_df = df.loc[[df.index[-1]]+list(df.index[:-1])].reset_index(drop=True)

output:

   0
9  9
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8

Alternate method:

new_df = pd.concat([df[df["Year week"]==52], df[~(df["Year week"]==52)]])
Tom McLean
  • 5,583
  • 1
  • 11
  • 36
  • thank you for your! Actually it puts week 53 at the top (great!) but in my case it replaces the first row instead of shifting it down. Moreover the same row (week 53 is still at the bottom). – sawabcn Jun 23 '21 at 10:55
  • Can you add your data to your question as text by doing `df.to_clipboard()`, then I can help further. – Tom McLean Jun 23 '21 at 10:58
  • I've also slightly modified my code, if you want to try that – Tom McLean Jun 23 '21 at 11:00
  • week 2020 2021 evo 53 0.0 90164.97000000004 inf 2 153009.8099999999 296288.24000000005 0.9364002870142785 3 156174.4200000004 298381.84000000026 0.9105679406396996 .. 14 35924.22999999998 0.0 -1.0 53 0.0 90164.97000000004 inf – sawabcn Jun 23 '21 at 11:09
  • sorry looks horrible as text but I exceed the characters if I paste the whole table. Basically I see week 53 as first row and last row (it copied the row instead of moving it) moreover the raw with week 1 was replaced by row week 53 – sawabcn Jun 23 '21 at 11:11
  • @sawabcn You can press "edit" under your question and put the data there. I slightly changed the code in my question, did my modified code work okay? – Tom McLean Jun 23 '21 at 11:13
  • Also added another method – Tom McLean Jun 23 '21 at 11:18
0

Since you can't insert the row and push others back directly, a clever trick you can use is create a new order:

# adds a new column, "new" with the original order
df['new'] = range(1, len(df) + 1)

# sets value that has index 53 with 0 on the new column
# note that this comparison requires you to match index type
# so if weeks are object, you should compare df.index == '53'
df.loc[df.index == 53, 'new'] = 0

# sorts values by the new column and drops it
df = df.sort_values("new").drop('new', axis=1)

Before:

         numbers
weeks           
1      181519.23
2       18507.58
3       11342.63
4        6064.06
53       4597.90

After:

         numbers
weeks           
53       4597.90
1      181519.23
2       18507.58
3       11342.63
4        6064.06
rosaqq
  • 426
  • 4
  • 16
  • Thank you! In this case I just want week 53 to be at the top and then kepp the descending order, I need to move only the last row to the top not sorting the index. – sawabcn Jun 23 '21 at 10:58
  • Updated answer to do what you requested – rosaqq Jun 23 '21 at 11:08