我有下表:
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
在此处应用了各种过滤器,例如日期/年龄/职业/ M-F /已婚-未婚
截至目前,所有过滤器值均来自CSV文件。我想将默认视图发布为:
Date Income
01-Feb-15 155,000
01-Mar-15 60,000
然后根据过滤器值更改日期和收入列。
答案 0 :(得分:0)
只需按Date
和Income
分组即可获得总和:
grouped = df.groupby('Date')['Income'].sum().reset_index()
导致:
Date Income
0 01-Feb-15 155
1 01-Mar-15 60
编辑:
为了在获得总和之前过滤数据帧,只需在分组之前设置过滤器:
df = df[df['Marreid/Unmarried']=='Married']
然后如上所述:
grouped = df.groupby('Date')['Income'].sum().reset_index()
导致:
Date Income
0 01-Feb-15 120