Why do I get weakref in pandas reassignment?

Viewed 109

I want to get rid of rows whose index isin the list ["A", "C"].

Example:

import pandas as pd
df_test = pd.DataFrame({"Col": [1,2,3,4]}, index=["A", "B", "C", "D"])
   Col 
A   1 
B   2 
C   3 
D   4

Now I put reassignment:

df_test = df_test[~df_test.index.isin(["A", "C"])]

I'm trying to understand the following result.

df_test._is_copy
(* <weakref at 0x11d14a4a8; to 'DataFrame' at 0x11d0dbac8> *)
  1. why do I get weakref copy here?

  2. Is it a right way to eliminate rows whose indexes are in the given list? Should I use .loc[:,:] on the left or .copy() on the right?

1 Answers

I believe .drop is the preferred method (and doesn't generate your copy error). I'm not sure why though

df_test = df_test.drop(['A', 'C'])

enter image description here

Related