I am trying to figure out how I can apply cumulative functions to objects. For numbers there are several alternatives like cumsum
and cumcount
. The
I think you can use cumsum with exception set
, then you need first convert to list
and then to set
. Btw, storing set
(C2
) or lists
of lists
(C4
) in columns in DataFrame
is not recommended.
print df
C1 C2 C3 C4
0 1 {A} A [A]
1 2 {B} B [B]
2 3 {C} C [C]
3 4 {D} D [D]
print df[['C1','C3','C4']].cumsum()
C1 C3 C4
0 1 A [A]
1 3 AB [A, B]
2 6 ABC [A, B, C]
3 10 ABCD [A, B, C, D]
df['C2'] = df['C2'].apply(list)
df = df.cumsum()
df['C2'] = df['C2'].apply(set)
print df
C1 C2 C3 C4
0 1 {A} A [A]
1 3 {A, B} AB [A, B]
2 6 {A, C, B} ABC [A, B, C]
3 10 {A, C, B, D} ABCD [A, B, C, D]