Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reverse order of entire DataFrame

I’m working with a dataset on historical electricity prices for Ameren. The way the data is currently structured is in the reverse order of what I want. That is, it starts with the price for October 2022 and ends with the price for January 2017. It looks like this:

     Month  Residential Non-Space Heat  ...  Usage above 800 kWh    PEA
0   Oct-22                      12.236  ...                9.777  0.249
1   Sep-22                      10.628  ...                  NaN  0.021
2   Aug-22                      10.628  ...                  NaN -0.048
3   Jul-22                      10.628  ...                  NaN -0.053
4   Jun-22                      10.623  ...                  NaN -0.032
..     ...                         ...  ...                  ...    ...
65  May-17                       6.519  ...               -0.519    NaN
66  Apr-17                       6.519  ...               -0.523    NaN
67  Mar-17                       6.519  ...               -0.448    NaN
68  Feb-17                       6.519  ...               -0.322    NaN
69  Jan-17                       6.519  ...               -0.321    NaN

[70 rows x 5 columns]

What I’m wanting to do is reorder the dataframe so that it starts with Jan-17 and ends with Oct-22. I tried dataset.iloc[::-1] but it when I do print(dataset) it doesn’t show that anything has changed. I also tried to use sort_values on the Month column, but It started ordering them like Oct-22, Oct-21, Oct-20,…Sept-22, Sept-21, and so on. Instead, I’m wanting it to be ordered like Jan-17, Feb-17, Mar-17,…, Sept-22, Oct-22. How would I reorder this dataframe in the desired way? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to re-assign your dataframe :

dataset = dataset.iloc[::-1]

# Output :

print(dataset)

     Month Residential Non-Space Heat  ... Usage above 800 kWh    PEA
69  Jan-17                      6.519  ...              -0.321    NaN
68  Feb-17                      6.519  ...              -0.322    NaN
67  Mar-17                      6.519  ...              -0.448    NaN
66  Apr-17                      6.519  ...              -0.523    NaN
65  May-17                      6.519  ...              -0.519    NaN
..     ...                        ...  ...                 ...    ...
4   Jun-22                     10.623  ...          NaN -0.032   None
3   Jul-22                     10.628  ...          NaN -0.053   None
2   Aug-22                     10.628  ...          NaN -0.048   None
1   Sep-22                     10.628  ...                 NaN  0.021
0   Oct-22                     12.236  ...               9.777  0.249

If the index is not important, use dataset = dataset.iloc[::-1].reset_index(drop=True).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading