Created
May 18, 2020 07:10
-
-
Save mithunkamat/8e5e6240aa57b8d32c70b64f05a21f52 to your computer and use it in GitHub Desktop.
Example of renaming columns using a 'mappings' dictionary which is dynamically built from a 'mapping' table from within an Excel sheet.
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
data = pd.read_csv(mappings['Filepath']) | |
# lowercase and strip whitespace from all column headers | |
data.rename({col: col.strip().lower() for col in data.columns}, | |
axis=1, inplace=True) | |
# we must also do the same for inv_mappings | |
# also add snake_case formatting to internal columns (optional) | |
inv_mappings = { | |
mappings[key].strip().lower(): | |
key.strip().lower().replace(' ', '_') | |
for key in mappings | |
} | |
# and now we can safely rename input data columns | |
data.rename(inv_mappings, axis=1, inplace=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment