Search code examples
pythonpandasdataframein-place

DataFrame inplace modification with return value


I am reading Wes McKinney's 'python for data analysis' and saw the following code

# Always returns a reference to a DataFrame
_ = data.rename(index={'Ohio':'Indiana'}, inplace=True)

Is there a reason to set the inplace change to _?


Solution

  • _ is usually meant to label a variable we don't care about/don't want to use.

    This usually makes more sense when you pull out a tuple and don't care about all the values e.g.

    a, _ = (1, 2)  # pulls out a == 1
    

    In this case, with a single value, there is no reason to do it. You can safely clean this code to:

    data.rename(index={'Ohio':'Indiana'}, inplace=True)
    

    I suspect this is a refactor from:

    data = data.rename(index={'Ohio':'Indiana'})
    

    which would make the comment "Always returns a reference to a DataFrame" make sense (when you use inplace it returns None rather than a DataFrame!).