Shivpe_R - 3 months ago 22

Python Question

I Have a Data Frame as descibed Below

`Dt_Frame = pd.DataFrame()`

AIDList = ['ID1','ID2','ID3','ID4','ID5']

BIDList = ['ID1','ID2','ID3']

Dt_Frame = Dt_Frame.append ({'Country': 'USA', 'Schedule': 'Daily', 'Date': '2016-12-07', 'Status': 'Active','AListIDs' : AIDList ,'BListIDs' : BIDList}, ignore_index=True)

I have a add a Column

`Difference`

`'ID4,'ID5'`

`Numb_Items`

Answer

To add new column, you can `Dt_Frame["newColumnName"] = value`

.

Regarding the `set`

difference, your intuition is right. First, you can use `apply`

to convert the `list`

's into `set`

's

```
A = Dt_Frame["AListIDs"].apply(set)
B = Dt_Frame["BListIDs"].apply(set)
```

Then applying minus on each side will give you the difference w.r.t the other set. That is

```
A - B
0 {ID4, ID5}
dtype: object
B - A
0 {}
dtype: object
```

For symmetric difference we'll need `A,B`

to be in the same `DataFrame`

(either for the `symmetric_difference`

method or for the `|`

operator):

```
# We add two new columns
Dt_Frame["ASetIDs"] = A
Dt_Frame["BSetIDs"] = B
# We need to transpose since apply operates on columns
Dt_Frame[["ASetIDs", "BSetIDs"]].T.apply(lambda x: x.ASetIDs.symmetric_difference(x.BSetIDs))
```

Source (Stackoverflow)