I want to convert the AVG column's numbers to hour(s):minute(s) form.
plus I want to show the AVG time of these activities on a bar. All the activities. I us this for it:
df2 = df.groupby(['Activity']).mean()
I want to see the hour:minutes
df = pd.read_csv('practice.csv')
df['AVG'] = df['AVG'].astype('datetime64[ns]')
out:
Activity AVG 0 sleep 1970-01-01 00:00:00.000000221 1 sleep 1970-01-01 00:00:00.000000201 2 swim 1970-01-01 00:00:00.000000300 3 run 1970-01-01 00:00:00.000001768 4 tv 1970-01-01 00:00:00.000000105
First aggregate mean
, convert column to timedeltas and format it in custom function:
df2 = df.groupby('Activity', as_index=False)['AVG'].mean()
#https://stackoverflow.com/a/51102096/2901002
def f(x):
ts = x.total_seconds()
hours, remainder = divmod(ts, 3600)
minutes, seconds = divmod(remainder, 60)
return ('{}:{:02d}').format(int(hours), int(minutes))
df2['AVG'] = pd.to_timedelta(df2['AVG'], unit='s').apply(f)