1

I appended 3 rows to a dataframe with this code: z = pandas.DataFrame.append(z, [{'Items': 'Foo'}, {'Items': 'Barr'}, {'Items': 'Options'}]) and got the below result:

    Items          Last 12 Months  
0   Apple          48.674  
1   Dragon Fruit   8.786  
2   Milk           2.367  
3   Foo            3.336  
4   Barr           0.005  
5   Options        NaN  

Is it possible to move the last 3 rows "Foo, Barr, Options" to become the first 3 rows instead? Without changing the index..

1 Answer 1

1

How about reindex?

In [4]: df
Out[4]: 
          Items  Last 12 Months
0         Apple          48.674
1  Dragon Fruit           8.786
2          Milk           2.367

In [5]: z = pd.DataFrame([{'Items': 'Foo', 'Last 12 Months': 3.336}, {'Items': 'Barr', 'Last12Months': 0.005}, {'Items': 'Options'}])

In [6]: z
Out[6]: 
     Items  Last 12 Months
0      Foo           3.336
1     Barr           0.005
2  Options             NaN

In [7]: df = df.append(z, ignore_index = True)

In [8]: df
Out[8]: 
          Items  Last 12 Months
0         Apple          48.674
1  Dragon Fruit           8.786
2          Milk           2.367
3           Foo           3.336
4          Barr           0.005
5       Options             NaN

In [9]: prev_len = len(df) - len(z)

In [10]: df = df.reindex(range(prev_len, len(df)) + range(prev_len))

In [11]: df
Out[11]: 
          Items  Last 12 Months
3           Foo           3.336
4          Barr           0.005
5       Options             NaN
0         Apple          48.674
1  Dragon Fruit           8.786
2          Milk           2.367
1
  • Hmm, But there is a total of 670 rows.. I've just given a snippet of the entire population.
    – jake wong
    Commented Aug 20, 2016 at 4:09

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.