Python pandas: Multi-column filter using ~df.type.isin and !=

Viewed 64

I am a very new to python and I am learning python to automate some excels workflows for my business.

df = pd.DataFrame({ "Index": list(range(8)),
                    "Name": ["Joe", "Tom", "Joe", "Tom", "Joe", "Tom", "Joe", "Tom"],
                    "Age": [23, 54, 34, 42, 23, 54, 42, 42]})

   Index Name  Age
0      0  Joe   23
1      1  Tom   54
2      2  Joe   34
3      3  Tom   42
4      4  Joe   23
5      5  Tom   54
6      6  Joe   42
7      7  Tom   42

I am trying to return the data for Not "Tom" AND Not "42", that is to say I want to filter out rows 3 and 7 while keeping the rest.

I have used the following codes to try and filter out rows that are not Tom AND not 42.

df = df[(df.Name != "Tom") & (df.Age != 42)]

df = df[~df.Name.isin(["Tom"]) & ~df.Age.isin([42])]

However, the output I am getting is:

   Index Name  Age
0      0  Joe   23
2      2  Joe   34
4      4  Joe   23

However, the output I want is:

   Index Name  Age
0      0  Joe   23
1      1  Tom   54
2      2  Joe   34
4      4  Joe   23
5      5  Tom   54
6      6  Joe   42

Any suggestions? Thank you

2 Answers

You're very close to what you need.

df = df[~((df['Name']=='Tom') & (df['Age']==42))]

print(df)

   Index Name  Age
0      0  Joe   23
1      1  Tom   54
2      2  Joe   34
4      4  Joe   23
5      5  Tom   54
6      6  Joe   42

In that case it should be Not "Tom" or Not "42"

df = df[(~(df.Name=="Tom")) | (~(df.Age==42))]
print(df)
   Index Name  Age
0      0  Joe   23
1      1  Tom   54
2      2  Joe   34
4      4  Joe   23
5      5  Tom   54
6      6  Joe   42
Related