Multiindex Roll-up Indicator

时间:2018-04-26 16:52:38

标签: python pandas multi-index

如何通过日期和时间汇总多指数? ID和创建指标?

+--------+-----+------+-------------+
|  Date  | ID  | Flag | Action Type |
+--------+-----+------+-------------+
| 201712 | 123 | -    | Delete      |
| 201712 | 456 | +    | Add         |
| 201712 | 123 | +    | Add         |
| 201801 | 123 | +    | Change      |
+--------+-----+------+-------------+

输出:

+--------+-----+------+--------------+
|  Date  | ID  | Flag | Action Type  |
+--------+-----+------+--------------+
| 201712 | 123 | *    | Add & Delete |
| 201712 | 456 | +    | Add          |
| 201801 | 123 | +    | Added Chg    |
+--------+-----+------+--------------+

1 个答案:

答案 0 :(得分:0)

您可以使用groupbyjoin

s=df.groupby(['Date','ID'],as_index=False).agg('&'.join)
s.Flag.str.len().gt(1)
Out[285]: 
0     True
1    False
2    False
Name: Flag, dtype: bool
s.loc[s.Flag.str.len().gt(1),'Flag']='*'
s
Out[287]: 
     Date   ID Flag  Actiontype
0  201712  123    *  Delete&Add
1  201712  456    +         Add
2  201801  123    +      Change