I have a dataframe with timeseries data
| Timestamp | Values |
|---|---|
| 10-26-22 10.00 AM | 1 |
| 10-26-22 09.04 AM | 5 |
| 10.26-22 10.06 AM | 6 |
| -------- | -------- |
| 10-27-22 3.32 AM | 9 |
| 10-27-22 3.36 PM | 5 |
| 10-27-22 3.31 PM | 8 |
| -------- | -------- |
| 10-27-22 3.37 AM | 8.23 |
| 10-28-22 4.20 AM | 7.2 |
I tried to sort the timestamp column into ascending order by :
df.sort_values("Timestamp", ascending = True, inplace= True)
but this code is not working. I want to get the data like this:
| Timestamp | Values |
|---|---|
| 10-26-22 09.04 AM | 1 |
| 10-26-22 10.00 AM | 5 |
| 10-26-22 10.06 AM | 6 |
| -------- | -------- |
| 10-27-22 3.31 AM | 9 |
| 10-27-22 3.32 PM | 5 |
| 10-27-22 3.36 PM | 8 |
| ------ | -------- |
| 10-27-22 3.37 AM | 8.23 |
| 10-28-22 4.20 AM | 7.2 |
CodePudding user response:
I guess you'll need to drill down to the timestamp then convert the format before using the sort_values function on the dataframe..
You should look through the documentation. This is scarcely implemented.
CodePudding user response:
You can use the key parameter of sort_values to convert the string to datetime internally with to_datetime and the %m-%d-%y %H.%M %p format:
df.sort_values(by='Timestamp', key=lambda s: pd.to_datetime(s, format='%m-%d-%y %H.%M %p'))
Output:
Timestamp Values
1 10-26-22 09.04 AM 5.00
0 10-26-22 10.00 AM 1.00
2 10-26-22 10.06 AM 6.00
5 10-27-22 3.31 PM 8.00
3 10-27-22 3.32 AM 9.00
4 10-27-22 3.36 PM 5.00
6 10-27-22 3.37 AM 8.23
7 10-28-22 4.20 AM 7.20
Alternatively, assign a dummy column:
(df.assign(key=pd.to_datetime(df['Timestamp'], format='%m-%d-%y %H.%M %p'))
.sort_values(by='key')
)
