0

I have a dataframe as:

df:
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|     |   Unnamed: 0 | country                 | league                       | game                                               | home_odds   |   draw_odds |   away_odds |   home_score | away_score   | datetime            |
+=====+==============+=========================+==============================+====================================================+=============+=============+=============+==============+==============+=====================+
|   0 |            0 | Chile                   | Primera Division             | Nublense - A. Italiano                             | 2.25        |        3.33 |        3.11 |            1 | 0            | 2021-06-08 00:30:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   1 |            1 | China                   | Jia League                   | Zibo Cuju - Shaanxi Changan                        | 11.54       |        4.39 |        1.31 |          nan | nan          | 2021-06-08 08:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   2 |            2 | Algeria                 | U21 League                   | Medea U21 - MC Alger U21                           | 2.38        |        3.23 |        2.59 |          nan | nan          | 2021-06-08 09:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   3 |            3 | Algeria                 | U21 League                   | Skikda U21 - CR Belouizdad U21                     | 9.48        |        4.9  |        1.25 |          nan | nan          | 2021-06-08 09:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+
|   4 |            4 | China                   | Jia League                   | Zhejiang Professional - Xinjiang Tianshan          | 1.2         |        5.92 |       12.18 |          nan | nan          | 2021-06-08 10:00:00 |
+-----+--------------+-------------------------+------------------------------+----------------------------------------------------+-------------+-------------+-------------+--------------+--------------+---------------------+

I have defined datetime as datetime

df['datetime'] = pd.to_datetime(df['datetime'])

and then tried to sort it

df.sort_values(by=['datetime'], ascending=True)

However the sorting does not work.

Can anybody help me understand why?

Please find the entire dataframe here for reference.

p.s. I am unable to paste the entire dataframe here because of character constraints.

6
  • You did df = df.sort_values(by=['datetime'], ascending=True) and you just only copied the command, or you didn't update the variable with the sorted dataframe that was returned? Commented Jun 9, 2021 at 0:11
  • I dont understand Commented Jun 9, 2021 at 0:13
  • sort_values (by default) returns a new dataframe with the values sorted. So I was confirming that you did something with that sorted copy. Like, for example, update the df variable with it. Commented Jun 9, 2021 at 0:14
  • 1
    Aha! So I have to reference it as df = df.sort_values(by=['datetime'], ascending=True) right? Yep. That works! Thats silly of me. Thank you! Commented Jun 9, 2021 at 0:15
  • 1
    What do you mean? sort_values returns a value. You would use it like any function that returned a value. new_df = df.sort_values..., df = df.sort_values... print(df.sort_values...) etc. Commented Jun 9, 2021 at 0:20

1 Answer 1

1

I see in the comments you already found your solution. Copying the df back into itself after calling sort_values() means it's "new" name is the old name.

I'll add this as an answer.

df.sort_values(by=['datetime'], ascending=True, inplace=True)

Then it should make the sorting in-place, so you don't have to assign it to itself.

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.