首页 > 解决方案 > 为什么我在熊猫重新分配中得到弱引用?

问题描述

我想摆脱索引isin列表的行["A", "C"]

例子:

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

现在我重新分配:

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

我试图理解以下结果。

df_test._is_copy
(* <weakref at 0x11d14a4a8; to 'DataFrame' at 0x11d0dbac8> *)
  1. 为什么我weakref在这里得到副本?

  2. 消除索引在给定列表中的行是否正确?我应该用.loc[:,:]左边还是.copy()右边?

标签: pythonpython-3.xpandasdataframe

解决方案


我相信 .drop 是首选方法(并且不会产生您的复制错误)。我不知道为什么

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

在此处输入图像描述


推荐阅读