-1

This is an extension of this question

using

 df['new_col'] = zip(df.lat, df.long)

to combine multiple columns into tuple

how to drop (or not add) empty elements (column) that is tuple should contain only non-empty values

just to give an example:

instead of ('a','') or ('a',)
display     ('a')

also, is there a way to convert the tuple to a set

Community
  • 1
  • 1
muon
  • 12,821
  • 11
  • 69
  • 88
  • shouldn't `df1 = df.dropna(subset=['lat','long']) df['new_col'] = zip(df1['lat'], df1['long'])` work? also what are you intending to display where 1 or both cols are null? – EdChum Feb 07 '16 at 20:39
  • just empty tuple, I just used the example from linked thread, I am working on different applicaition – muon Feb 07 '16 at 20:41
  • That seems to make a little sense: with your example, the output type is either a tuple or a single element, depending on the row. You can't have two output types together for a column. –  Feb 07 '16 at 20:52
  • 1
    Tuple to set: `set(mytuple)`? –  Feb 07 '16 at 20:52

1 Answers1

0

Thanks for the helpful comments guys, Here's something that worked for me:

import copy
def remove_empty(x):
    for c in copy.copy(x): 
        if not c: 
            x.discard(c)
    return x

df['new_col'] = zip(df.lat, df.long)
df['new_col'] = df['new_col'].apply(set)
df['new_col'] = df['new_col'].apply(remove_empty)
muon
  • 12,821
  • 11
  • 69
  • 88