gpt4 book ai didi

python - 从 Pandas Dataframe 的列中删除 URL

转载 作者:行者123 更新时间:2023-11-30 22:09:12 24 4
gpt4 key购买 nike

我有一个小数据框,正在尝试从链接列中字符串的末尾。我尝试过以下代码,它适用于 url 独立的列。问题是,只要 url 之前有句子,代码就不会删除这些 url

这是数据:https://docs.google.com/spreadsheets/d/10LV8BHgofXKTwG-MqRraj0YWez-1vcwzzTJpRhdWgew/edit?usp=sharing (链接到电子表格)

import pandas as pd  

df = pd.read_csv('TestData.csv')

df['Links'] = df['Links'].replace(to_replace=r'^https?:\/\/.*[\r\n]*',value='',regex=True)

df.head()

谢谢!

最佳答案

试试这个:

import re
df['cleanLinks'] = df['Links'].apply(lambda x: re.split('https:\/\/.*', str(x))[0])

输出:

df['cleanLinks']

cleanLinks
0 random words to see if it works now
1 more stuff that doesn't mean anything
2 one last try please work

关于python - 从 Pandas Dataframe 的列中删除 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51994254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com