johnbowen
johnbowen

Reputation: 121

Return Sum for Single Day using Pandas

I am working with a CSV file and I need to return the sum of data for a specific day. Thus far I have been able to break the code into this:

import panda as pd
df = pd.read_csv (r'C:Users\ern\Downloads\evergreen.csv')
sum_imps = df['money'].sum() #sum the total of the money column
sumimps_byday = df.groupby(['day'])[["money"]].sum() #groups the sum of the money column by day

Now all i need is to be able to take it one step further and rerun the sum of money for a a specific day of my choosing. I don't think this is too hard, just drawing a blank.

Upvotes: 0

Views: 62

Answers (2)

Samuel Nde
Samuel Nde

Reputation: 2743

Let us define df as follows:

import pandas as pd
df = pd.DataFrame(data = [['day1', 2900, 3000], ['day2', 3300, 3350], ['day3', 3200, 3150], ['day1', 3200, 3050]], columns = 'day, money, close'.split(', '))

Here is what df looks like.

df
>>>
     day    money   close
0   day1    2900    3000
1   day2    3300    3350
2   day3    3200    3150
3   day1    3200    3050

Suppose I want to get the sum of money for day which we can easily see to be 6100, I would do the following.

df[df.day == 'day1']['money'].sum()
>>>6100

Upvotes: 0

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39830

total_money = df.loc[df['day'] == '20/03/2019', 'money'].sum()

should do the trick.

For example,

import pandas as pd

df = pd.DataFrame({'day': ['20/03/2019', '21/03/2019', '20/03/2019'],
                   'money': [1, 5, 7]})
print(df)

print('Total money for 20/03/2019: ' + str(df.loc[df['day'] == '20/03/2019', 'money'].sum()))

should give the desired output

          day  money
0  20/03/2019      1
1  21/03/2019      5
2  20/03/2019      7
Total money for 20/03/2019: 8

Upvotes: 1

Related Questions