Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active December 16, 2016 13:35
Show Gist options
  • Save anddam/bd1c3cd57055d7de729646a7731bdad7 to your computer and use it in GitHub Desktop.
Save anddam/bd1c3cd57055d7de729646a7731bdad7 to your computer and use it in GitHub Desktop.
from pandas import DataFrame, concat
from numpy import nan
correct = DataFrame({'foo': ((1, 'a'), (2, 'b')),
'bar': ((nan, 123), (3, 7)),
'valid_1': (144, 169),
'valid_2': (12, 13)})
wrong = DataFrame({'foo': (),
'bar': (),
'valid_1': (),
'valid_2': ()})
def purge_dataframe(df):
"Split column containing tuples in separate columns"
keep = df[['valid_1', 'valid_2']]
foo = DataFrame(df.foo.tolist(), columns=['numbers', 'letters'], index=df.index)
bar = DataFrame(df.bar.tolist(), columns=['what', 'ever'], index=df.index)
return concat((keep, foo, bar), axis=1)
print(purge_dataframe(correct))
print(purge_dataframe(wrong))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment