【发布时间】:2017-07-09 04:50:23
【问题描述】:
我有一个数据集,其中包含 tweetID、tweet-text、RegExp1、RegExp2、RegExp3、RegExp4 列和 4 个正则表达式的列表。 我想在 tweet-text 列上一一应用正则表达式,如果 tweet-text 满足正则表达式,那么我想在相应的 RegExp 列中将值设置为 1,如果不满足则我想将其设置为 0 .
例如,假设 tweet-text 满足正则表达式编号 1,那么我想将对应的 RegExp1 列的值设置为 1,不满足正则表达式 2,那么我想将对应的 RegExp2 列的值设置为 0,依此类推。我尝试了最后给出的代码,但它对我不起作用。
我的数据集看起来像
tweetID | tweet-text | RegExp1 | RexExp2 | RegExp3 | RexExp4
---------------------------------------------------------------------
10001 | to get it or? | | | |
10333 | I just wonder :) | | | |
10933 | is it possible dude| | | |
14633 | he is good at | | | |
代码:
`regexes = [
re.compile('i asked .* said'),
re.compile('you asked me what .*'),
re.compile('(to get|to see|to look|is it true|is it possible) .*'),
re.compile('I .* wonder .*')
]
for regex, i in zip(regexes, range(4)):
columnName = "RegExp"+str(i+1)
for row in df['tweet-text']:
if(regex.search(row) != None):
df[columnName] = 1
else:
df[columnName] = 0`
(最好使用熊猫)谢谢
【问题讨论】:
-
那么到底难在哪里呢?你试过为它写代码吗?
-
我尝试了很多次......但没有成功...... - @HarshithThota
-
所以,如果您可以发布您尝试过的代码,我们可以帮助您解决问题。
-
好的,那么您的数据集在哪里?你的正则表达式是什么?在不帮助我们了解您想要做什么的情况下,您如何期望任何帮助?
-
@Irfanullah 1. 细节不应该出现在 cmets 中。 2. 我们也需要查看您的数据集。 csv 或 pandas 框架的剪贴板粘贴。
标签: python regex python-3.x pandas dataframe