Skip to content

Instantly share code, notes, and snippets.

@swayson
Created September 1, 2015 13:04
Show Gist options
  • Save swayson/e872b14370ea687f9a6f to your computer and use it in GitHub Desktop.
Save swayson/e872b14370ea687f9a6f to your computer and use it in GitHub Desktop.
Simple function to select (or reorder) columns of a pandas DataFrame
def select(dataframe, columns, keep_others=True):
''' Re-order or select columns. If keep_others, then it is simply re-ordered else it will select columns'''
cols = set(dataframe.columns)
if keep_others:
others = list(cols.difference(columns))
reordered = columns + others
return dataframe[reordered]
else:
return dataframe[columns]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment