Home > Software engineering >  Pandas: how to add each row of a column the value with each other
Pandas: how to add each row of a column the value with each other

Time:03-14

csv file with for each date the earning

In the image you see a csv file, I want to add for each date, in the example 01-01-2020, the earnings of that date witch eachother and also for the other date, so an example for 01-01-2020 is the total earnings 11.50. So i can use in bokeh on the x-axis the date and on the y-axis the total amount of that date. How can i achieve that?

CodePudding user response:

You can use the groupby to obtain what you described

df('date')['earning'].sum()

CodePudding user response:

Something like might work:

>>> date = ['01-01-2020', '01-01-2020', '02-01-2020', '02-02-2020','03-02-2020','03-02-2020']
>>> df = pd.DataFrame({'earning': earnings, 'date': date})
>>> df
   earning        date
0     10.0  01-01-2020
1      1.5  01-01-2020
2      6.4  02-01-2020
3      5.4  02-02-2020
4      7.8  03-02-2020
5      9.7  03-02-2020
>>> df.groupby(['date'])['earning'].sum()
date
01-01-2020    11.5
02-01-2020     6.4
02-02-2020     5.4
03-02-2020    17.5
Name: earning, dtype: float64

You could then use the plot function on the resulting dataframe from sum operation.

  • Related