[英]How to add quotes to the start and end of a link in Python?
I have a dataframe with links and I want to add quotes to the end and start of the quotes.我有一个带有链接的 dataframe,我想在引号的末尾和开头添加引号。 This is how my df looks like:
这就是我的 df 的样子:
links
https://us.search.yahoo.com
https://us.search.google.com
https://us.search.wikipedia.com
I want my output to be:我希望我的 output 是:
links
'https://us.search.yahoo.com'
'https://us.search.google.com'
'https://us.search.wikipedia.com'
Thank you in advance!先感谢您!
This is relatively crude but given you've not supplied an example it's the best I can come up with;这是相对粗略的,但鉴于您没有提供示例,这是我能想到的最好的;
import pandas as pd
df = pd.DataFrame({'link': 'https://us.search.yahoo.com', 'text': 'Yahoo', 'title': 'Yahoo Search - Web Search'}, index=[0])
print(df["link"].apply(repr))
Output: Output:
'https://us.search.yahoo.com'
I was curious about the impact of using the inefficient apply
method here~很好奇这里使用了低效的
apply
方式会有什么影响~
Given:鉴于:
link text title
0 https://us.search.yahoo.com Yahoo Yahoo Search - Web Search
I explode its size to 1 million rows:我将它的大小分解为 100 万行:
df = pd.concat([df]*int(1e6), ignore_index=True)
And then run some timing tests:然后运行一些计时测试:
%timeit df.link.apply(repr)
93.5 ms ± 2.35 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit "'" + df.link + "'"
68.3 ms ± 1.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
It's clear that the vectorized version of this, "'" + df.link + "'"
is significantly faster, but it's unlikely to make a practical difference unless your dataframe is insanely large.很明显,
"'" + df.link + "'"
的矢量化版本明显更快,但除非您的 dataframe 非常大,否则不太可能产生实际影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.