Copy a DataFrame column containing sets

Viewed 41

I would like to copy a column from a DataFrame into a new column. The Problem is that it is containing sets.

xy=pd.DataFrame(columns=[2])
xy.at[1,2]={1,2,3,4}
xy[3]=xy[2].copy()
xy.at[1,3].add(5)

I would like to get this:

           2                 3
1   {1, 2, 3, 4}      {1, 2, 3, 4, 5}

But xy is returning this:

           2                 3
1   {1, 2, 3, 4, 5}   {1, 2, 3, 4, 5}

How could I Keep the set in column 2 unchanged? Thanks

3 Answers

You can get the desired result by changing the last line to

xy.at[1,3] = set(xy.at[1,3]).union(set([5]))

|operator to concatenate two sets:

xy=pd.DataFrame(columns=[2])
xy.at[1,2]={1,2,3,4}
xy[3]=xy[2].copy()
xy.at[1,3]=xy.at[1,3]|{5}

print(xy)
              2                3
1  {1, 2, 3, 4}  {1, 2, 3, 4, 5}

The problem here is that a set is a mutable object. When you use xy[2].copy you only make a shallow copy of a Series so the copy will still reference the same set.

You are left with 2 options:

  • force a deep copy (not really simple: copy.deepcopy would not be enough)

    xy[3] = [s.copy() for s in xy[2]]    # forces a copy of the sets
    
  • never mutate a set, but assign a new one:

    xy.at[1,3] = xy.at[1,3] | {5}
    

    Beware: xy.at[1,3] |= {5} would actually mutate the set...

Related