Created
September 1, 2015 13:04
-
-
Save swayson/e872b14370ea687f9a6f to your computer and use it in GitHub Desktop.
Simple function to select (or reorder) columns of a pandas DataFrame
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
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