How do I replace multiple column names with different values? Ideally I want to remove certain characters in column names and replace others.
I have to run my jupyter notebook twice in order to get this code to work. Does anyone know the reason for this? Also, how would I go about simplifying this code (I am aware of just nesting .replace(), however that doesn't solve my problem). The snippet posted below may not be enough to go off of; please view the following link to my notebook if needed: https://datalore.jetbrains.com/notebook/iBhSV0RbfC66p84tZsnC24/w3Z6tCriPC5v5XwqCDQpWf/
for col in df.columns:
df.rename(columns={col: col.replace('Deaths - ', '').replace(' - Sex: Both - Age: All Ages (Number)', '')}, inplace=True)
df.rename(columns={col: col.replace(' (deaths)', '')}, inplace=True)
df.rename(columns={col: col.replace(' ', '_')}, inplace=True)
for col in df.columns:
df.rename(columns={col: col.lower()}, inplace=True)
CodePudding user response:
aside from replace, as you had done here are few other ways
# create a list of columns and assign to the DF
cols = ['col1','col2','col3','col4']
df.columns=df.columns = cols
or
# create a dictionary of current values to the new values
# and update using map
d = {'c1' : 'col1', 'c2': 'col2', 'c3':'col3' , 'c4':'col 4'}
df.columns.map(d)
CodePudding user response:
I can't comment due to 'points' but firstly - avoid inplace
- https://github.com/pandas-dev/pandas/issues/16529#issuecomment-443763553
For the replacement of multiple values in multiple columns you can use:
df = df.rename(columns = { ... }).rename(columns = lambda x: x.lower())
Which will rename using the given dictionary, and convert to lower case.