0

I recently started to use pandas but I had some confusion over it's pointer and "inplace" operation. Related posts can be found here: Pandas - inplace, view, copy confusion and Python Pandas - Understanding inplace=True

Suppose A is a data frame.

Here's what I read from the text.

A.drop(["col_name1"], axis=1, inplace=True) 

#operated on A directly and drop the col_name1

B=A.drop(["col_name1"], axis=1 ) 

# return a copy of dropped value and assigned to B, thus B was not a pointor

However, if I type

B=A.drop(["col_name1"], axis=1, inplace=True) 

will B be a pointer of A?

Further, when will the command .copy() be necessary? i.e. where we must use .copy() so that B was an extra copy but not a pointer.

B=A["col_names_n"].operation.copy()

Why does pandas use pointer so often? i.e. if I type the

B=1
A=B
A=2
print(B)
#then it would return 1

But pandas's default was to declare B as a pointer.

  • Python *does not have pointers*. – juanpa.arrivillaga May 12 '19 at 21:22
  • @juanpa.arrivillaga I thought so, but if I try B=A in pandas, then I change value in B or A will change values in A or B. That's by default a pointer, or something alike. – ShoutOutAndCalculate May 12 '19 at 21:24
  • This might help. https://stackoverflow.com/questions/27673231/why-should-i-make-a-copy-of-a-data-frame-in-pandas – run-out May 12 '19 at 22:20
  • All variables work *exactly the same*. Every variable acts like a reference. You can think of it as a pointer, and it sort of works like a pointer that is automatically dereferenced for you, but it isn't a pointer. You can't dereference it, you cant do pointer arithmetic etc. In any case, this has *nothing* to do with `pandas`. All variables in Python work this way. Every single time you do `a = b`, that simply says that the variable `a` now references the *same object* that is being referenced by `b` at that time. Read the following: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga May 13 '19 at 22:48

0 Answers0