Reputation: 15
I want to group my dataframe so that the rows with the same hour from timestamp column (which has data like 2019-01-01 00:00:00.134721167,50,100 where 50 is the cost, and 100 is percentage) have their cost summed and averaged, as well as percentage.
Or, to be more specific, i need to have 48 rows for 2 days of information, one for each hour, while now i have more than 500 rows. How do I do that?
Upvotes: 0
Views: 205
Reputation: 21709
Here's a way to do:
# sample data
df = pd.DataFrame({'date': pd.date_range("2019-01-01", freq='H', periods = 10),
'cost': pd.np.random.randint(10, 100, 10)})
Method 1:
df.set_index('date').resample('H').sum()
Method 2:
df.groupby(pd.Grouper(key='date', freq='H'))['cost'].sum().reset_index()
Upvotes: 1