Reputation: 8247
I have following dataframe in pandas
ID description log
1 104 motherboardsrno123
2 104 displaysrno456
3 107 1234
4 108 53455
5 104 pulsersrno2333
6 108 53456
7 109 NA
I want to keep only those rows which contains 'mother' or 'pulser' in description 104
Following is my desired dataframe
ID description log
1 104 motherboardsrno123
3 107 1234
4 108 53455
5 104 pulsersrno2333
6 108 53456
7 109 NA
I am doing following in pandas
df= df[(df.log.str.contains("mother|pulsar",na=True)) and (df.description == '104')]
But this is not giving me required output.
Upvotes: 1
Views: 48
Reputation: 862406
It seems you need change second string to pulser
, change and
to &
for bitwise AND and if necessary compare by integer 104
(not string '104'
, but it depends of data):
df = df[(df.log.str.contains("mother|pulser", na=True)) & (df.description == 104)]
print (df)
ID description log
0 1 104 motherboardsrno123
4 5 104 pulsersrno2333
For your expected output - get mother or pulser
or not 104
:
df= df[(df.log.str.contains("mother|pulser", na=True)) | (df.description != 104)]
print (df)
ID description log
0 1 104 motherboardsrno123
2 3 107 1234
3 4 108 53455
4 5 104 pulsersrno2333
5 6 108 53456
6 7 109 NaN
Upvotes: 1