Home > Enterprise >  How to sum up values in amount with duplicate date rows?
How to sum up values in amount with duplicate date rows?

Time:09-23

How to sum up values in amount with duplicate rows?

For example there are 2 unique dates. Can we do groupby? I'm expecting result like

Date         Amount
2019-07-01   20000000.....
2019-07-02   3000000000.....

The dataframe is below :

Date          Amount
2019-07-01    2,055,359.9800
2019-07-01    2,055,359.9800
2019-07-01    145198200
2019-07-01    145198200
2019-07-02    1,924,232.7200
2019-07-02    137,860,984.9000
2019-07-02    137,466,690.8800
2019-07-02    138,102,066.0400
2019-07-02    1,928,055.4400

CodePudding user response:

Yes, you can groupby the Date column and use sum to get the total Amount for each group.

import pandas as pd

df = pd.DataFrame({'Date': ['2019-07-01', '2019-07-01', '2019-07-01',
                            '2019-07-02', '2019-07-02', '2019-07-02'],
                   'Amount': [1, 2, 3, 4, 5, 6]})
print(df)

df2 = df.groupby(['Date']).sum()
print(df2)

Output:

            Amount
Date              
2019-07-01       6
2019-07-02      15

CodePudding user response:

Yes we can:

df = pd.DataFrame(...)

sums = df.groupby(['Date']).sum()
  • Related