python - Pandas 如果行值包含列表中的项目作为子字符串,则将行值保存到不同的数据框
问题描述
如果行值包含列表中的项目作为子字符串,则将行值保存到不同的数据框。
输入数据帧:
index link
1 https://zeewhois.com/en/
2 https://www.phpfk.de/domain
3 https://www.phpfk.de/de/domain
4 https://laseguridad.online/questions/1040/pued
list=['verizon','zeewhois','idad']
如果 df['link'] 有任何项目 list
作为子字符串,我们需要将其放在link
不同的新数据框中。
到目前为止,我已经对该link
列进行了预处理并购买了这种格式:
index link
1 httpszeewhoiscomenwww
2 httpswwwphpfkdedomain
3 httpswwwphpfkdededomain
4 httpslaseguridadonlinequestions1040pued
查找哪些行值包含list
子字符串
中的项目df["TRUEFALSE"] = df['link'].apply(lambda x: 1 if any(i in x for i in list) else 0)
但我得到了错误:
TypeError: 'in <string>' requires string as left operand, not float
解决方案
你可以使用 str.contains
list_strings =['verizon','zeewhois','idad']
df.loc[df.link.str.contains('|'.join(list_strings),case=False), 'TRUE_FALSE'] = True
index link TRUE_FALSE
1 https://zeewhois.com/en/ True
2 https://www.phpfk.de/domain NaN
3 https://www.phpfk.de/de/domain NaN
4 https://laseguridad.online/questions/1040/pued True
然后只需过滤 True 以获取您的新数据框
new_df = df.loc[df.TRUE_FALSE == True].copy()
index link TRUE_FALSE
1 https://zeewhois.com/en/ True
4 https://laseguridad.online/questions/1040/pued True
推荐阅读
- c - 使用带有 TMS320F2812 的 TI CCS v3.3 如何在 DSP 的闪存和导出的 bin 文件中的特定地址定义特定数据?
- python-3.x - 使过滤一个巨大的表更有效
- java - 如果值匹配,如何使测试用例失败
- c# - c#类型的面板,想自由放置标签
- python - Python:如何在没有键的情况下制作排序的跳过/删除字典?
- ios - 如何在不下载新版本 Xcode 的情况下下载 iOS-DeviceSupport 文件
- python - 右键单击并使用 Selenium Python 执行操作
- sed - 带有双 s 的 Sed 表达式
- c# - Size.Width 和 Size.Height 显示不正确的尺寸
- php - 为显示的每个 MYSQL 行添加不同的样式