使用条件从数据帧中删除字符串

2024-05-18 10:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有数据

                                          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'


Tags: httpseventhttpdfuidtimeaddressru
1条回答
网友
1楼 · 发布于 2024-05-18 10:17:32

试试这个:

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

相关问题 更多 >