0

Can someone please help me understand how to change the original df in my example? (link below). The function makes the changes that I want it to make however, I want it to update the original df that I input into the function. As seen in the code when I call df after the function it returns the original df and not the adjusted one from the function...

Any help greatly appreciated!

How to get function to change original df?

jcuk
  • 1
  • Welcome to Stack Overflow. Please read [ask], and include relevant code in the question itself, [as text and not as an image](https://meta.stackoverflow.com/questions/285551). Anyway, the problem is that writing `df = pd.merge(...` **does not change** the object that was named `df` at the beginning of the function (by being passed as a parameter). It creates a **new** object (via `pd.merge`), then causes `df` to *stop* naming the old object and *start* naming the new one. This *has nothing to do with Pandas* - that's *how `=` works* in Python. – Karl Knechtel May 10 '22 at 13:40
  • You can work around this either by getting the Pandas functionality to work in-place (this starts with reading the documentation, and may or may not be possible in general), or by accepting that it works this way and re-assigning the result after the function call. See also https://nedbatchelder.com/text/names1.html to make sure you understand how it works. – Karl Knechtel May 10 '22 at 13:44
  • It is also a good idea to [try to look for solutions first](https://meta.stackoverflow.com/questions/261592), for example by using a search engine. I found a duplicate by searching for `dataframe merge "in place"` and linked it here. It has some other reference links in the question. I hope it will be sufficient. – Karl Knechtel May 10 '22 at 13:45

0 Answers0