Python - Drop rows where column does not equal variable

Viewed 16

i am trying to drop rows where the value in column does not equal the variable.

I have used the following to create the variable, which determines if the current month is in H1 or H2.

current_month =date.today().month     
Halfyear = np.where(current_month < 7,'H1','H2')

Which works as for today (september) month 9 it produces a variable of H2

I then have the following to add an extra column "Half year" to the dataframe and then drop rows that does not equal the variable Halfyear

checks['Check Date']=pd.to_datetime(checks['Check Date'],dayfirst=True)
checks['Half year'] =np.where(checks['Check Date'].dt.month.le(6),'H1','H2')
checks.drop(checks.loc[checks['Half year'] != Halfyear].index, inplace = True)

which produces the following error on the drop line. Buffer has wrong number of dimensions (expected 1, got 0

if i replace "Halfyear" with H2 , all the rows with H1 are correctly deleted.

My question is how do i amend the code to drop rows matching the variable i created, or is this not possible?

0 Answers
Related