-4

I have below table:

Date       Age    Income  profession  M/F Marreid/Unmarried
01-Feb-15  20-25  35,000  IT Enginner  M  Unmarried
01-Mar-15  25-30  45,000  IT Enginner  F  Unmarried
01-Feb-15  30-35  50,000  IT Enginner  M  Married
01-Feb-15  35-40  70,000  Doctor       M  Married
01-Mar-15  30-35  15,000  Servent      M  Unmarried

There are various filter applied on this e.g. Date/Age/Profession/M-F/Married-Unmarried

All filter values as of now are comming from CSV file. Howver i want to publish the default view as :

Date       Income  
01-Feb-15  155,000
01-Mar-15  60,000

then based on filter value Date and Income column will be changed.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Rocky
  • 25
  • 1
  • 5

1 Answers1

0

Just group by Date and Income and get the sum:

grouped = df.groupby('Date')['Income'].sum().reset_index()

resulting in:

        Date  Income
0  01-Feb-15     155
1  01-Mar-15      60

EDIT:

in order to filter the dataframe before getting the sum, just set the filter before grouping:

df = df[df['Marreid/Unmarried']=='Married']

and then as above:

grouped = df.groupby('Date')['Income'].sum().reset_index()

resulting in:

        Date  Income
0  01-Feb-15     120
Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122
  • Thanks that worked.. I am at Pandas's learner stage. Thanks for corporating on such basics. I have another q: if i want to show another column for Expenditure along with Income. How can i do that? – Rocky Sep 12 '15 at 16:05
  • You're welcome. Please accept the answer if it worked well for your purposes. – Fabio Lamanna Sep 12 '15 at 16:12
  • How can i filter this data based on values coming from front end like user may just want to see the data for Females or unmarried males or IT married females. – Rocky Sep 14 '15 at 15:31
  • Output will be same as given in Question : Date Income 01-Feb-15 155,000 01-Mar-15 60,000 Values should be changed based on filter values. Does it make sense? – Rocky Sep 14 '15 at 16:52
  • I've just updated the answer, please tell me if it is what you wanted. – Fabio Lamanna Sep 14 '15 at 20:30
  • Thanks. This i already did, i also did for many filters(combination of 2 or 3) . How can i implement below conditions: 1) If only one filter is selected e.g. Male then all other filters should take all the values corresponding to Only Male filter 2) If only two filters are selected e.g. Only Male and IT profession are selected.. then all values correspoding to only these two filters should be given. – Rocky Sep 14 '15 at 20:44
  • Sorry but I can't figured out what you want. Consider to ask a new question with sample input data and desired output. – Fabio Lamanna Sep 14 '15 at 20:47
  • Ok.. when i am trying to access the columns (Date, Income) of resulted table ... then it gives me error as Date is not identified. Does using Sum or Group by looses the column names? – Rocky Sep 17 '15 at 01:05
  • Actually grouping the dataframe lead to have the `Date` as index of the new dataframe, and the `Income` as values. So you can access them through `grouped.index` and `grouped.values`. Hope that helps. I update also the answer with a new way to do it. – Fabio Lamanna Sep 17 '15 at 08:00
  • Thanks that worked. I also want to draw a Bar graph and Line chart on one. X-axis should list the Date and Bars will show the M and F salaries plotted on Y axis and their expenses as line charts on another Y axis. How can i do that? – Rocky Sep 22 '15 at 00:16
  • @Rocky please avoid asking new questions in the comments. Consider post another new complete question, thanks. – Fabio Lamanna Sep 22 '15 at 08:46
  • I am not able to enter new Questions... as Question was related to same..so thought to post here only. – Rocky Sep 22 '15 at 12:29