基于“不在"从数据帧中删除行.健康)状况 [英] dropping rows from dataframe based on a "not in" condition

查看:72
本文介绍了基于“不在"从数据帧中删除行.健康)状况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当date列的值在日期列表中时,我想从pandas数据框中删除行.以下代码不起作用:

I want to drop rows from a pandas dataframe when the value of the date column is in a list of dates. The following code doesn't work:

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

df=df[df.datecolumn not in a]

我收到以下错误:

ValueError:系列的真值不明确.使用a.empty,a.bool(),a.item(),a.any()或a.all().

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

推荐答案

您可以使用

You can use pandas.Dataframe.isin.

pandas.Dateframe.isin将返回布尔值,具体取决于每个元素是否在列表a中.然后,您可以使用~对其进行反转,以将True转换为False,反之亦然.

pandas.Dateframe.isin will return boolean values depending on whether each element is inside the list a or not. You then invert this with the ~ to convert True to False and vice versa.

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

这篇关于基于“不在"从数据帧中删除行.健康)状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆