1

I have a dataframe with date column. Once I sort by date, I expect to read data out of dataframe with sort order. However, using iloc only gives the sorted data but using loc seems to retain the sequence prior to sorting. Is there a way to circumvent this issue. I want to use 'loc' but get sorted data out of dataframe.

import pandas as pd
client_dictionary = {'name': ['Michael', 'Ana', 'Sean'],
                     'country': ['UK', 'UK', 'USA'],
                     'age': [10, 51, 13],
                     'latest date active': ['07-05-2019', '23-12-2019', '03-04-2016']}
df = pd.DataFrame(client_dictionary)
df.head()
df['latest date active'] = df['latest date active'].astype('datetime64[ns]')
df.sort_values(by='latest date active', inplace=True)
df.reset_index()

for i in range(0, len(df)):
    print("With loc: " + str(df.loc[i, 'latest date active']) + ", with iloc: " + str(df.iloc[i, 3]))

When the above code is run, I get the below output:

With loc: 2019-07-05 00:00:00, with iloc: 2016-03-04 00:00:00
With loc: 2019-12-23 00:00:00, with iloc: 2019-07-05 00:00:00
With loc: 2016-03-04 00:00:00, with iloc: 2019-12-23 00:00:00
Noel
  • 57
  • 4
  • 2
    Does this answer your question? [How are iloc, ix and loc different?](https://stackoverflow.com/questions/31593201/how-are-iloc-ix-and-loc-different) – E. Zeytinci Dec 29 '19 at 11:08

1 Answers1

0

You must either reassign the DataFrame or pass an inplace argument when resetting the index:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reset_index.html

df.sort_values(by='latest date active', inplace=True)
df.reset_index(inplace=True)

Now they should be equivalent.

fsl
  • 3,250
  • 1
  • 10
  • 20