0

New to using Python. I'm loading a csv file into a pandas dataframe - I then want to amend one of the column headers as shown below.

Loading csv file:

df = (pd.read_csv(file, sep=',', error_bad_lines=False, index_col=False, encoding='cp1252', low_memory=False,
dtype={'ID':'string',
'Address':'string',
'Postcode / Zipcode':'string')

Can't get this rename header code to work. When I print the df the column name remains unchanged.

df.rename(columns={"Postcode / Zipcode" : "Postal Code"})

There are 4 csv files that I will be loading using a loop and the name of the column will vary for each file.

CGarden
  • 169
  • 17
  • 1
    Is that exactly how you're using rename? because rename is not inplace by default. `df = df.rename(columns={"Postcode / Zipcode" : "Postal Code"})` or `df.rename(columns={"Postcode / Zipcode" : "Postal Code"}, inplace=True)`. – Henry Ecker Jul 30 '21 at 21:04
  • 1
    @Henry Ecker yes that's exactly how it's coded sadly. Used your first option suggested and has worked. So thank you. Basic mistake from me. Thanks again. – CGarden Jul 30 '21 at 21:10
  • 2
    As above. And note when looking up the functions on pages like (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rename.html) and the examples at the bottom they may show a change but not a permanent change (if the command ran twice the changes would disappear). You either assign the change or, if available, you can use `inplace=True`. Just to complicate things `inplace` may be deprecated in the future (but OK for now and lot's use it) – MDR Jul 30 '21 at 21:10
  • 1
    @HenryEcker happy to mark this as the answer if you add it as such. – CGarden Jul 31 '21 at 20:38
  • Does this answer your question? [Renaming column names in Pandas](https://stackoverflow.com/questions/11346283/renaming-column-names-in-pandas) – sarrysyst Jul 31 '21 at 21:39

1 Answers1

1

DataFrame.replace is not an in-place operation by default.

The default arguments are:

DataFrame.replace(to_replace=None,
                  value=None, 
                  inplace=False,  # <- inplace is False by Default!
                  limit=None,
                  regex=False,
                  method='pad')

Either assign back to update the value in df:

df = df.rename(columns={"Postcode / Zipcode" : "Postal Code"})

Or set inplace=True:

df.rename(columns={"Postcode / Zipcode" : "Postal Code"}, inplace=True)
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57