0

I want to rename a few columns and set inplace = False then I am able to view the result. Why set inplace = True will result in None (I have refresh the runtime to run inplace=True)?

ckd_renamed = ckd_data.rename(
    columns = {"id": "Id",
               "age": "Age",
               "bp": "blood_pressure"},
    inplace=True)

The dataset that I use is "https://raw.githubusercontent.com/dphi-official/Datasets/master/Chronic%20Kidney%20Disease%20(CKD)%20Dataset/ChronicKidneyDisease.csv"

dfrankow
  • 20,191
  • 41
  • 152
  • 214
Azul
  • 25
  • 8
  • 1
    Does this answer your question? [Understanding inplace=True](https://stackoverflow.com/questions/43893457/understanding-inplace-true) – Mike Scotty Sep 28 '21 at 11:20
  • `inplace=True` means that the dataframe stored in the `ckd_data`variable is modified *in-place*. `None` is returned because no new dataframe is constructed; instead, the "old" `ckd_data` variable now holds the result of that operation. This is similar to how `some_list.append(1)` does not return a new list but rather modifies `some_list` in-place. – jfaccioni Sep 28 '21 at 11:20
  • @jfaccioni, inplace=True is the modified version is being saved to the assigned variable. If setting it to True will result in None as no new modified can be display then what is the point for us to set it to True or False since it will save the modified version to the assigned variable.? – Azul Sep 28 '21 at 11:38
  • @MikeScotty, thanks for the suggestion. The conclusion from this article is that it is better to avoid it if possible as it will create problem if using with chaining method and increase memory consumption. However, the purpose of this post is to find out why setting it to True will display None. This means there is nothing new to display. The default setting for inplace is False. When set it to False and call the variable assigned during the renaming, it will display the modified version. So, it will auto saved the new modified version. I confusing with the purpose of having it. – Azul Sep 28 '21 at 11:52

1 Answers1

1

Because when you set inplace=True, there is no new copy created, and ckd_data is modified, well, in-place.

The convention of returning None for in-place modifications stems from how e.g. Python's .sort() works.

After

ckd_data.rename(columns = {"id": "Id", "age": "Age", "bp": "blood_pressure"}, inplace=True)

you'd continue to refer to the dataframe, now with modified column names, as ckd_data.

EDIT: If you're using Colab and you want the cell to also display the new value, you should be able to add an additional expression statement with just the name of the variable.

ckd_data.rename(columns = {"id": "Id", "age": "Age", "bp": "blood_pressure"}, inplace=True)
ckd_data
AKX
  • 152,115
  • 15
  • 115
  • 172
  • each time I change the inplace, I will refresh my runtime to run again. So, the dataset is still in the original form. – Azul Sep 28 '21 at 11:23
  • I don't know what you mean with "refresh my runtime". – AKX Sep 28 '21 at 11:25
  • I am using Google Colab to run my code so refresh the runtime will allow Colab "forget" all my original setting in the code. In short, like refresh the page. – Azul Sep 28 '21 at 11:40
  • See my edit. You never mentioned you're using Colab in your original post, you know. – AKX Sep 28 '21 at 11:43