gpt4 book ai didi

python - 如果列值的组合等于列表中的元组,则删除 Pandas 中的 dataFrame 行

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:22 25 4
gpt4 key购买 nike

我目前这样做是为了删除具有特定列 'some_column' 值的行,该值在列表 removal_list

中找到
df = df[~df['some_column'].isin(removal_list)]

如果我想比较元组列表中的值组合,我该怎么做? (如果有更好的方法来实现,不一定需要是元组列表)

例如:

removal_list = [(item1,store1),(item2,store1),(item2,store2)]

如果特定行的 df['column_1']df['column_2'] 具有值 item1store1 (或 removal_list 中的任何其他元组),然后删除该行

另外,可能有超过两列需要评估

编辑更好的例子:

client  account_type    description
0 1 2 photographer
1 2 2 banker
2 3 3 banker
3 4 2 journalist
4 5 4 journalist

remove_list = [(2,journalist),(3,banker)]

检查列 account_typedescription

输出:

client  account_type    description
0 1 2 photographer
1 2 2 banker
4 5 4 journalist

最佳答案

说你有

removal_list = [(item1,store1),(item2,store1),(item2,store2)]

然后

df[['column_1', 'column_2']].apply(tuple, axis=1)

应该创建一系列的元组,所以

df[['column_1', 'column_2']].apply(tuple, axis=1).isin(removal_list)

是您所追求的二进制条件。删除与之前相同。这应该适用于任意数量的列。

示例

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
>>> df[['a', 'b']].apply(tuple, axis=1).isin([(1, 3), (30, 40)])
0 (1, 3)
1 (2, 4)
dtype: object

关于python - 如果列值的组合等于列表中的元组,则删除 Pandas 中的 dataFrame 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50370467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com