1

I have a dataframe like this :

        Date          Price
0 2021-02-12 00:00:00 50
1 2021-02-11 00:00:00 2
2 2021-02-10 00:00:00 40.4
3 2021-02-09 00:00:00 775.6
4 2021-02-08 00:00:00 1000.2
5 2021-02-07 00:00:00 500

Columns' types with print(df.dtypes) :

Date         datetime64[ns]
Price               float64

Expected Output :

        Date          Price
0 2021-02-07 00:00:00 500
1 2021-02-08 00:00:00 1000.2
2 2021-02-09 00:00:00 775.6
3 2021-02-10 00:00:00 40.4
4 2021-02-11 00:00:00 2
5 2021-02-12 00:00:00 50

I have a dataframe like df. When I do :

df = df.sort_values(by='Date')

But nothing happen even by adding ascending = True or False.

Could you give the way pls to order this dataframe as above ?

If possible can you give the 2 possibilites like ordering by index and date but I'm looking to order by ascending date directly without touching to the index.

EDIT for more clarity :

# Converted list of dictionaries to a Dataframe
extracted_data_List_DataFrames = [pd.DataFrame(x) for x in extracted_data_List]

# Convert string to their respectiv types
for dfs in extracted_data_List_DataFrames:
    dfs['Date'] = pd.to_datetime(dfs['Date'])
    dfs['Price'] = dfs['Price'].astype('float64')

    # Sort dataframes by 'Date'
    dfs = dfs.sort_values(['Date'], ascending=False)

print(extracted_data_List_DataFrames)

You have my code above. I'm not able to make the sort method to work correctly.

0

2 Answers 2

2

Problem is if modify values in loop there is no change if original list, you can assign ouput to original list of DataFrames like:

for i, dfs in enumerate(extracted_data_List_DataFrames):
    dfs['Date'] = pd.to_datetime(dfs['Date'])
    dfs['Price'] = dfs['Price'].astype('float64')

    # Sort dataframes by 'Date'
    dfs = dfs.sort_values(['Date'], ascending=False)
    extracted_data_List_DataFrames[i] = dfs

Another idea is use inplace=True:

for dfs in extracted_data_List_DataFrames:
    dfs['Date'] = pd.to_datetime(dfs['Date'])
    dfs['Price'] = dfs['Price'].astype('float64')

    # Sort dataframes by 'Date'
    dfs.sort_values(['Date'], ascending=False, inplace=True)
2
  • Thank you for your answer. It's working well. I saw on some other post that inplace will be deprecated is that right ?
    – LuckyFr
    Feb 15, 2021 at 9:16
  • @LuckyFr - Yes, it should be, so add first alternative without inplace=True. But it is still used of many people (inplace) so only suggestion yet for deprecated.
    – jezrael
    Feb 15, 2021 at 9:24
0

sort_values() sorts the data in ascending order by default. You are interested in .sort_values(by='Date', ascending=False):

import pandas as pd

df = pd.DataFrame(range(10), columns=['Price'])

df['Date'] = [pd.to_datetime(f"2021-02-{str(x).zfill(2)}") for x in range(1, 11)]

df.sort_values(by=['Date'], ascending=False)
2
  • doesn't work otherwise I would not ask the question. Thank you
    – LuckyFr
    Feb 14, 2021 at 17:06
  • Hmm, which panda version do you have? The above works with version 1.2.2. Feb 14, 2021 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.