Mathieu
Mathieu

Reputation: 761

Drop rows on condition in pandas dataframe

I want to remove rows which contains "True" in the column "aff".

Expected result : Wikipedia.org raw (line) is removed

My csv:

website;aff;cond;ger;azs
wikipedia.org;True;False;False;True
youtube.com;False;False;False;False
amazon.es;False;False;False;False

My code:

check = pd.read_csv(os.path.join(path2, "my.csv"), sep=";")
output = output.merge(check, on=["Domain"], how="left") 
output = output.loc[~(output["aff"] == "True")].reset_index(drop=True)

Print of output gives:

Domain            aff          cond      ger         azs
wikipedia.org     True         False     False       True
youtube.com       False        False     False       False
amazon.es         False        False     False       False

Print of output["aff"] gives:

0            True
1            False
2            False
3            False

Upvotes: 0

Views: 64

Answers (2)

user14487166
user14487166

Reputation:

You put the Boolean value in quotations so it is comparing your values to a string, not the True value. Your method works without the quotes. In fact, it works without comparing the 'aff' series to a True value since you're working with booleans.

Upvotes: 1

Danail Petrov
Danail Petrov

Reputation: 1875

I guess this is what you're looking for?

>>> !cat test.csv
website;aff;cond;ger;azs
wikipedia.org;True;False;False;True
youtube.com;False;False;False;False
amazon.es;False;False;False;False

>>> df=pd.read_csv('test.csv', sep=';')

>>> df

         website    aff   cond    ger    azs
0  wikipedia.org   True  False  False   True
1    youtube.com  False  False  False  False
2      amazon.es  False  False  False  False


>>> df[~df['aff']]

       website    aff   cond    ger    azs
1  youtube.com  False  False  False  False
2    amazon.es  False  False  False  False

I am only guessing here as there is no desired output posted, but based on your explanation - this should do it for you.

Upvotes: 1

Related Questions