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