Home > Enterprise >  Deleting a variable in pandas from a df
Deleting a variable in pandas from a df

Time:01-09

I'm having a problem trying to delete a variable from a data frame. I have the first line of code working with classifying routine_tasks with two conditions, but when I go to drop using the variable it drops everything in the df. The context of what I am trying to do is drop routine tasks from a maintenance board.

routine_tasks = mboard.loc[((mboard['Down Date']== '2021-06-20')&(mboard['Category']== 'Maintaince'))]
Down = mboard.drop(routine_tasks, axis = 1)

I've tried using a different axis, I've looked up different uses to drop.

CodePudding user response:

You need to pass indices to drop:

mask = ((mboard['Down Date'] == '2021-06-20')
       &(mboard['Category'] == 'Maintaince'))

routine_tasks = mboard.loc[mask]

Down = mboard.drop(mboard.index[routine_tasks])

Alternatively, use boolean indexing:

mask = ((mboard['Down Date'] == '2021-06-20')
       &(mboard['Category'] == 'Maintaince'))

routine_tasks = mboard.loc[mask]

Down = mboard.loc[~mask]

CodePudding user response:

To drop the rows in mboard that are contained in routine_tasks, you can use the ~ operator to negate the boolean mask generated by the 'isin' method:

Down = mboard.drop(mboard[mboard.isin(routine_tasks)].index, axis=0)

This will drop the rows in mboard that are contained in routine_tasks.

Alternatively, you can also use the index attribute of 'routine_tasks' to drop the rows from 'mboard':

Down = mboard.drop(routine_tasks.index, axis=0)
  • Related