petercheung
petercheung

Reputation: 79

Pandas how can I dropping rows by multiple conditions or from a defined variable

I have a problem in python pandas.

My Excel file input is:

TEAM
001
001
002
003
004

I want to improve my coding, how can I drop TEAM 001, 002, ... in one command or how can I define a variable dropteam = '001','002' to delete 001 and 002?

Like this the but this code is wrong dropteam001 = replacename[~replacename['TEAM'].str.contains(dropteam)]

My code:

dropteam001 = replacename[~replacename['TEAM'].str.contains('001')]
dropteam002 = dropteam001[~replacename['TEAM'].str.contains('002')]

Upvotes: 0

Views: 45

Answers (2)

BENY
BENY

Reputation: 323356

You can do two in one time

dropteam = replacename[~replacename['TEAM'].str.contains('001|002')]

Upvotes: 1

rhug123
rhug123

Reputation: 8778

You could make a list of the items you want to drop and do:

dropteam001 = replacename[~replacename['TEAM'].isin(newlist)]

Upvotes: 0

Related Questions