【问题标题】:How to Remove Rows from DataFrame Based on Values in Series如何根据系列中的值从 DataFrame 中删除行
【发布时间】:2019-07-10 06:51:27
【问题描述】:

我知道这是一个简单的问题,但我就是找不到解决方法。

我有一个 DataFrame,我想根据另一个 series 中的值删除行。

X
   1   2   5   6   7   10  12  13
0   5   4   4   4   0   4   0   3
1   3   0   3   0   0   0   0   3
2   4   0   0   0   0   0   0   0
3   3   0   0   0   5   4   5   5
4   3   0   0   0   0   0   0   1 

Vtk
1    4
2    3
4    3
Name: rank, dtype: int64

我想从 X 中删除与 Vtk 中值 a = 3 的索引相对应的行。在这种情况下,我希望根据值 a = 3 删除 X 中索引为 24 的行。像这样:

X
   1   2   5   6   7   10  12  13
0   5   4   4   4   0   4   0   3
1   3   0   3   0   0   0   0   3
3   3   0   0   0   5   4   5   5

到目前为止我已经尝试过:

b = Vtk.isin([~a])
newX = X.loc[b]

但是有 IndexingError :

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

还有其他方法可以解决我的问题吗?

【问题讨论】:

    标签: python python-3.x pandas dataframe jupyter-notebook


    【解决方案1】:

    首先按Series 过滤索引值,然后按DataFrame.drop 删除行:

    b = Vtk.index[Vtk == 3]
    print (b)
    Int64Index([2, 4], dtype='int64')
    
    newX = X.drop(b)
    print (newX)
       1  2  5  6  7  10  12  13
    0  5  4  4  4  0   4   0   3
    1  3  0  3  0  0   0   0   3
    3  3  0  0  0  5   4   5   5
    

    另一种使用isin~ 过滤的解决方案:

    newX = X[~X.index.isin(b)]
    print (newX)
       1  2  5  6  7  10  12  13
    0  5  4  4  4  0   4   0   3
    1  3  0  3  0  0   0   0   3
    3  3  0  0  0  5   4   5   5
    

    通过loc 选择的解决方案是通过difference 获取索引值:

    newX = X.loc[X.index.difference(b)]
    print (newX)
       1  2  5  6  7  10  12  13
    0  5  4  4  4  0   4   0   3
    1  3  0  3  0  0   0   0   3
    3  3  0  0  0  5   4   5   5
    

    【讨论】:

    • 知道了,您的回复有帮助!谢谢!
    猜你喜欢
    • 2013-08-12
    • 2016-11-01
    • 2016-05-05
    • 2017-11-20
    • 2020-02-26
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多