简体   繁体   English

根据“不在”条件从数据框中删除行

[英]dropping rows from dataframe based on a “not in” condition

I want to drop rows from a pandas dataframe when the value of the date column is in a list of dates. 当日期列的值在日期列表中时,我想从pandas数据帧中删除行。 The following code doesn't work: 以下代码不起作用:

a=['2015-01-01' , '2015-02-01']

df=df[df.datecolumn not in a]

I get the following error: 我收到以下错误:

ValueError: The truth value of a Series is ambiguous. ValueError:Series的真值是不明确的。 Use a.empty, a.bool(), a.item(), a.any() or a.all(). 使用a.empty,a.bool(),a.item(),a.any()或a.all()。

You can use pandas.Dataframe.isin . 您可以使用pandas.Dataframe.isin

pandas.Dateframe.isin will return boolean values depending on whether each element is inside the list a or not. pandas.Dateframe.isin将返回布尔值,具体取决于每个元素是否在列表a You then invert this with the ~ to convert True to False and vice versa. 然后使用~将其反转为将True转换为False ,反之亦然。

import pandas as pd

a = ['2015-01-01' , '2015-02-01']

df = pd.DataFrame(data={'date':['2015-01-01' , '2015-02-01', '2015-03-01' , '2015-04-01', '2015-05-01' , '2015-06-01']})

print(df)
#         date
#0  2015-01-01
#1  2015-02-01
#2  2015-03-01
#3  2015-04-01
#4  2015-05-01
#5  2015-06-01

df = df[~df['date'].isin(a)]

print(df)
#         date
#2  2015-03-01
#3  2015-04-01
#4  2015-05-01
#5  2015-06-01

You can use Series.isin : 你可以使用Series.isin

df = df[~df.datecolumn.isin(a)]

While the error message suggests that all() or any() can be used, they are useful only when you want to reduce the result into a single Boolean value. 虽然错误消息表明可以使用all()any() ,但仅当您希望将结果减少为单个布尔值时,它们才有用。 That is however not what you are trying to do now, which is to test the membership of every values in the Series against the external list, and keep the results intact (ie, a Boolean Series which will then be used to slice the original DataFrame). 然而,这不是你现在要做的,即测试系列中每个值与外部列表的成员资格,并保持结果完整(即布尔系列,然后将用于切片原始DataFrame) )。

You can read more about this in the Gotchas . 您可以在Gotchas中阅读更多相关信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM