使用pandas从条件中删除带有条件的字符串

时间:2016-06-23 14:57:44

标签: python pandas

我有数据

                                          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   

我想删除str,其中url包含yandex.ru/clck。 欲望输出

                                          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  

我尝试使用~df[df.event_address.str.contains("yandex.ru/clck")],但它会返回

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

1 个答案:

答案 0 :(得分:2)

试试这个:

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

演示:

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
相关问题