NineWasps
NineWasps

Reputation: 2253

Delete string from dataframe with condition using pandas

I have data

                                          event_address           event_time  
0      https://mail.yandex.ru/clck=2230&uid=1189231...  2016-06-14 03:05:19   
1      https://mail.yandex.ru/?ncrnd=2230&uid=1189231...  2016-06-14 03:05:23   
2      https://mail.yandex.ru/clck=2230&uid=1189231...  2016-06-14 03:05:24   
3        http://mail.yandex.ru/?win=219&clid=2257587-216  2016-06-14 03:04:52   
4       https://mail.yandex.ru/?win=219&clid=2257587-216  2016-06-14 03:04:52   
5                     https://mail.yandex.ru/?ncrnd=2230  2016-06-14 03:05:12   
6      https://mail.yandex.ru/?ncrnd=2230&uid=1189231...  2016-06-14 03:05:25   

I want to delete str, where url contain yandex.ru/clck. Desire output

                                          event_address           event_time    
1      https://mail.yandex.ru/?ncrnd=2230&uid=1189231...  2016-06-14 03:05:23      
3        http://mail.yandex.ru/?win=219&clid=2257587-216  2016-06-14 03:04:52   
4       https://mail.yandex.ru/?win=219&clid=2257587-216  2016-06-14 03:04:52   
5                     https://mail.yandex.ru/?ncrnd=2230  2016-06-14 03:05:12   
6      https://mail.yandex.ru/?ncrnd=2230&uid=1189231...  2016-06-14 03:05:25  

I try use ~df[df.event_address.str.contains("yandex.ru/clck")]but it return

TypeError: bad operand type for unary ~: 'unicode'

Upvotes: 0

Views: 96

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

try this:

df[~df.event_address.str.contains(r"yandex.ru/clck")]

demo:

In [167]: df[~df.event_address.str.contains(r"yandex.ru/clck")]
Out[167]:
                                      event_address
1    https://mail.yandex.ru/?ncrnd=2230&uid=1189231
3   http://mail.yandex.ru/?win=219&clid=2257587-216
4  https://mail.yandex.ru/?win=219&clid=2257587-216
5                https://mail.yandex.ru/?ncrnd=2230
6    https://mail.yandex.ru/?ncrnd=2230&uid=1189231

In [168]: df
Out[168]:
                                      event_address
0      https://mail.yandex.ru/clck=2230&uid=1189231
1    https://mail.yandex.ru/?ncrnd=2230&uid=1189231
2      https://mail.yandex.ru/clck=2230&uid=1189231
3   http://mail.yandex.ru/?win=219&clid=2257587-216
4  https://mail.yandex.ru/?win=219&clid=2257587-216
5                https://mail.yandex.ru/?ncrnd=2230
6    https://mail.yandex.ru/?ncrnd=2230&uid=1189231

Upvotes: 2

Related Questions