2

My dataframe has columns like ticket, host, drive model, Chassis, Rack, etc.

I want all the rows with value in the Chassis column equal to '1025C-M3B', '1026T-M3FB', '2026TT-DLRF' or 'SYS-2027TR-D70RF+'. I want to delete the rest.

I tried

data2 = data1[data1.Chassis == '1025C-M3B' 
              or data1.Chassis == '1026T-M3FB' 
              or data1.Chassis == '2026TT-DLRF' 
              or data1.Chassis == 'SYS-2027TR-D70RF+']

Got

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

Then tried

data2 = data1[data1.Chassis.all() == '1025C-M3B' 
              or data1.Chassis.all() == '1026T-M3FB' 
              or data1.Chassis.all() == '2026TT-DLRF' 
              or data1.Chassis.all() == 'SYS-2027TR-D70RF+']

Got

KeyError: u'no item named False'

Can anyone please tell me how to do this?

1
  • use bitwise or (|) instead of logical or Commented Jun 1, 2015 at 19:52

2 Answers 2

2

Use bitwise or (|) instead of logical or.

data2 = data1[(data1.Chassis == '1025C-M3B') | (data1.Chassis == '1026T-M3FB') | (data1.Chassis == '2026TT-DLRF') | (data1.Chassis == 'SYS-2027TR-D70RF+')]

You can find plenty of reading material about the use of bitwise vs logical operations in numpy/pandas. Here is one.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use isin:

data1[data1['Chassis'].isin(['1025C-M3B', '1026T-M3FB', '2026TT-DLRF','SYS-2027TR-D70RF+'])]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.