Search code examples
pythonpandasmulti-index

Multiindex Roll-up Indicator


How do you roll-up multi-index by Date & ID and create indicators?

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

with an output of:

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

Solution

  • You can using groupby and join

    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