Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: resample dataframe and sum

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading