【问题标题】:filter list dataframe by element按元素过滤列表数据框
【发布时间】:2020-04-13 00:09:55
【问题描述】:

我有一个列表和数据框(下面的示例)。

                                     0  1
0                ((test1, AA), (1, 1))  1
1                ((test2, BB), (1, 1))  2
2                ((test1, CC), (1, 1))  3
3                ((test1, DD), (2, 1))  8
4                ((test3, EE), (3, 1))  9

我只需要过滤掉第一个元素为 test1 AND 1 的数据。你能帮忙吗?

预期输出:

                                     0  1
0                ((test1, AA), (1, 1))  1
2                ((test1, CC), (1, 1))  3

【问题讨论】:

    标签: python pandas list dataframe filter


    【解决方案1】:

    您可以使用布尔索引:

    v =  df[0].apply(lambda i: i[0][0] == 'test1' and i[1][0] == 1)
    df = df[v]
    print(df)
    

    输出

                           0  1
    0  ((test1, AA), (1, 1))  1
    2  ((test1, CC), (1, 1))  3
    

    【讨论】:

    • 解决方案是简化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 2017-12-15
    • 1970-01-01
    • 2018-07-14
    • 2019-03-07
    • 2023-03-17
    • 2021-12-20
    相关资源
    最近更新 更多