![](/img/trans.png)
[英]Is there a faster way I can count the number of occurrences of a number in a list?
[英]A faster way to count the number of Markdown links
我有一个包含 reddit 评论的 dataframe df
。 每个评论可能包含许多 Markdown 链接。 例如,
'[a](https://www.google.com/) bcd [e](https://pandas.pydata.org/)'
我想计算这样的链接。 我的方法是df['comment'].str.count('\[(?P<txt>[^\]]+)\]\((?P<lnk>[^\)]+)\)')
.
因为我的 dataframe 包含超过 400 万行,所以我想问一下是否有更快的方法。 太感谢了!
import pandas as pd
d = {'col1': [1, 2],
'comment': ['[a](https://www.google.com/) bcd [e](https://pandas.pydata.org/)',
'[f](https://www.google.com.vn/) bcd [g](https://pandas.pydata.org/)'
]
}
df = pd.DataFrame(data = d)
df['comment'].str.count('\[(?P<txt>[^\]]+)\]\((?P<lnk>[^\)]+)\)')
为了提高性能,我会放弃捕获组。
但是,正如 OP 在评论中所说,需要首先捕获组。 所以我在这里看到两种方法:
\[([^\]]+)\]\([^\)]+\)
然后替换模式应该是\1
(只是组参考)
\[[^\]]+\](?=\([^\)]+\))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.