删除特定值熊猫下方的行

时间:2018-08-01 06:14:18

标签: python pandas

此问题与附件类似,除了我没有任何 if (@toyear-@fromyear=0) BEGIN Set @vacsql = 'SELECT DISTINCT a.resourceID,r.EnterpriseID,r.FullName, dbo.fnGetWorkOrdersByResourceID(a.resourceID,'+ CAST(@tomonth AS VARCHAR(2))+','+CAST(@toyear AS VARCHAR(4)) +') as WorkOrder, dbo.fnGetDomainsByResourceID(a.resourceID,'+ CAST(@tomonth AS VARCHAR(2))+','+CAST(@toyear AS VARCHAR(4)) +') as Domain, l.Offshore_Ind,SUM(ISNULL(a.Totals,0)) AS TOTALHOURS FROM [Actuals] AS a join [Resources] AS r on a.ResourceID=r.ResourceID join Resource_Attributes as ra on r.ResourceID=ra.ResourceID join Location_LU as l on ra.LocationID=l.LocationID join Roll_On_Off as ro on r.ResourceID=ro.ResourceID WHERE (a.TaskName=''Leave'') AND a.CalYear='+CAST(@toyear as varchar(10))+' AND CalMonthNum <='+CAST(@tomonth as varchar(10))+' AND CalMonthNum >='+CAST(@frommonth as varchar(10))+' AND (CONVERT(datetime,ro.End_Dt))>(CONVERT(datetime,'''+cast(concat(@frommonth,'/01/',@toyear) as varchar(10))+''')) (CONVERT(datetime,ra.Start_dt))<=(CONVERT(datetime,@StartDate)) AND (CONVERT(datetime,ra.End_dt))>=(CONVERT(datetime,@EndDate)) GROUP BY ra.AttributeID,a.ResourceID,r.EnterpriseID,r.FullName,l.offshore_Ind' if exists (select 1 from Resource_Attributes ra where (CONVERT(datetime,ra.Start_dt))<=(CONVERT(datetime,@StartDate)) AND (CONVERT(datetime,ra.End_dt))>=(CONVERT(datetime,@EndDate)) AND CONVERT(datetime,ra.Start_dt) between (CONVERT(datetime,@StartDate)) AND (CONVERT(datetime,@EndDate))) BEGIN INSERT INTO @vacTable EXECUTE sp_executesql @vacsql, N'@StartDate varchar(25), @EndDate varchar(25)', @StartDate, @EndDate; END END 值。 How to delete row based on row above? Python Pandas

本质上,我只是想在特定值之后np.nan delete。对于下面的row,我想删除df之后的row

'Foo'

您可以将以下+1移位吗?

import pandas as pd
import numpy as np

d = ({
    'A' : ['X','Y','','X','Y'],           
    'B' : ['Foo',np.nan,'','Foo',np.nan],
    'C' : ['X','Y','','X','Y'],
    })

我的预期输出是:

dl = df.loc[df['B'] != 'Foo']

1 个答案:

答案 0 :(得分:1)

您需要更改等于shift的条件,并通过倒置布尔掩码进行过滤:

dl = df[~(df['B'] == 'Foo').shift().fillna(False)]
print (dl)
   A    B  C
0  X  Foo  X
2           
3  X  Foo  X