如何以特定格式呈现数据
How to present data in a particular format
我有以下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
对此应用了各种过滤器,例如Date/Age/Profession/M-F/Married-Unmarried
截至目前,所有过滤器值均来自 CSV 文件。但是我想将默认视图发布为:
Date Income
01-Feb-15 155,000
01-Mar-15 60,000
然后根据过滤器值更改日期和收入列。
只需按 Date
和 Income
分组并得出总和:
grouped = df.groupby('Date')['Income'].sum().reset_index()
导致:
Date Income
0 01-Feb-15 155
1 01-Mar-15 60
编辑:
为了在求和之前过滤dataframe,只需在分组之前设置过滤器:
df = df[df['Marreid/Unmarried']=='Married']
然后如上:
grouped = df.groupby('Date')['Income'].sum().reset_index()
导致:
Date Income
0 01-Feb-15 120
我有以下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
对此应用了各种过滤器,例如Date/Age/Profession/M-F/Married-Unmarried
截至目前,所有过滤器值均来自 CSV 文件。但是我想将默认视图发布为:
Date Income
01-Feb-15 155,000
01-Mar-15 60,000
然后根据过滤器值更改日期和收入列。
只需按 Date
和 Income
分组并得出总和:
grouped = df.groupby('Date')['Income'].sum().reset_index()
导致:
Date Income
0 01-Feb-15 155
1 01-Mar-15 60
编辑:
为了在求和之前过滤dataframe,只需在分组之前设置过滤器:
df = df[df['Marreid/Unmarried']=='Married']
然后如上:
grouped = df.groupby('Date')['Income'].sum().reset_index()
导致:
Date Income
0 01-Feb-15 120