I have two questions:
- I would like to reverse the order of the data loaded from the csv. Then get the time from the first row from the time column. After doing df = df[::-1], I expect the value 17:00:03 instead of 15:59:56. Where have I made a mistake?
- how to use Pandas to round the value 17:00:03 to 17:00:00?
My code:
df = pd.read_csv('gold_16_12_2021.csv')
print(df['time'].head())
print(df['time'][0])
df = df[::-1]
print(df['time'].head())
print(df['time'][0])
Result:
0 15:59:56
1 15:59:55
2 15:59:55
3 15:59:32
4 15:59:30
Name: time, dtype: object
15:59:56
79199 17:00:03
79198 17:00:05
79197 17:00:07
79196 17:00:07
79195 17:00:07
Name: time, dtype: object
15:59:56
Expect result:
17:00:03 or 17:00:00
CodePudding user response:
I think you need to reset the index using DataFrame.reset_index after reversing the order of the rows since the index doesn't get updated automatically. To round the second in the time, you can try using DataFrame.apply convert the time string in each row to datetime object and then format that object to string as %H:%M:00
(assuming you don't intend to round up the seconds if it's above 30) and then apply the result to either the time
column or a new column (round_time
in the below example)
import pandas as pd
from datetime import datetime
df = pd.read_csv('./test.csv')
print(df['time'].head())
print(df['time'][0])
df = df[::-1]
df.reset_index(inplace=True, drop=True)
print(df['time'].head())
print(df['time'][0])
df['round_time'] = df.apply(lambda row: datetime.strptime(row['time'], '%H:%M:%S').strftime("%H:%M:00"), axis=1)
print(df)