I have the following dataframe:
df=pd.DataFrame(index=[0,1])
df['timestamp'] = ['2022-01-01 20:10:00', '2022-01-01 20:50:00']
df['currency'] = ['USD', 'USD']
df['operation'] = ['deposit', 'deposit']
df['amount'] = [0.1, 0.4]
df:
timestamp currency operation amount
0 2022-01-01 20:10:00 USD deposit 0.1
1 2022-01-01 20:50:00 USD deposit 0.4
How can I resample the data on an hourly basis and sum the "amount" to get the following dataframe:
df:
timestamp currency operation amount
0 2022-01-01 20:00:00 USD deposit 0.5
Using .resample('H') eliminates the currency and operation columns. How can I do this so that Sum the "amount" column?
>Solution :
Doing with pd.Grouper and follow by agg
out = df.groupby(pd.Grouper(key='timestamp',freq='1h')).\
agg(lambda x : x.sum()
if x.dtypes == float
else x.iloc[0]).reset_index()
Out[122]:
timestamp currency operation amount
0 2022-01-01 20:00:00 USD deposit 0.5