Last active
December 16, 2016 13:35
-
-
Save anddam/bd1c3cd57055d7de729646a7731bdad7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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