如何用新名称替换特定标点符号?

时间:2016-11-16 08:42:49

标签: python pandas

我的数据样本是:

        comment sarc_majority
0        [?, ?]          sarc
1           [0]      non-sarc
2     [!, !, !]          sarc
3           [0]      non-sarc
4           [?]          sarc

我想用新名称替换标点符号。如 ? = punct1 ,! = punct2,' = punct3。我尝试使用csv文件中的read。

replace_df = pd.read_csv('./final/eng-mly-punct.csv', sep=',', quoting=csv.QUOTE_NONE,
                       names=["punct", "replacer"])
replace_df.head()

    punct   replacer
0   ?       punct1
1   !       punct2
2   '       punct3

然后我坚持要替换:

for punct, replacer in replace_df.itertuples(index=False,name=None):
    df.comment = df.comment.str.replace(r'\b{0}\b'.format(punct),replacer)

错误是:错误:无需重复

出了什么问题?或者有什么可能的方法吗? 所需的输出应该是:

                       comment sarc_majority
0             [punct1, punct1]          sarc
1                          [0]      non-sarc
2     [punct2, punct2, punct2]          sarc
3                          [0]      non-sarc
4                     [punct1]          sarc

提前致谢。欢呼声。

2 个答案:

答案 0 :(得分:1)

您可以使用d d replace - 但需要将?转义为\?

d = {'\?':'punct1','!':'punct2',"'":'punct3'}
df.comment = df.comment.replace(d, regex=True)
print (df)
                    comment sarc_majority
0          [punct1, punct1]          sarc
1                       [0]      non-sarc
2  [punct2, punct2, punct2]          sarc
3                       [0]      non-sarc
4                  [punct1]          sarc

您也可以从d创建replace_df

df = pd.DataFrame({'comment': {0: '[?, ?]', 1: '[0]', 2: '[!, !, !]', 3: '[0]', 4: '[?]'}, 'sarc_majority': {0: 'sarc', 1: 'non-sarc', 2: 'sarc', 3: 'non-sarc', 4: 'sarc'}})
print (df)
     comment sarc_majority
0     [?, ?]          sarc
1        [0]      non-sarc
2  [!, !, !]          sarc
3        [0]      non-sarc
4        [?]          sarc

replace_df = pd.DataFrame({'replacer': {0: 'punct1', 1: 'punct2', 2: 'punct3'}, 'punct': {0: '?', 1: '!', 2: "'"}})
print (replace_df)
  punct replacer
0     ?   punct1
1     !   punct2
2     '   punct3
replace_df.punct = '\\' + replace_df.punct
d = replace_df.set_index('punct')['replacer'].to_dict()
print (d)
{'\\!': 'punct2', "\\'": 'punct3', '\\?': 'punct1'}

df.comment = df.comment.replace(d, regex=True)
print (df)
                    comment sarc_majority
0          [punct1, punct1]          sarc
1                       [0]      non-sarc
2  [punct2, punct2, punct2]          sarc
3                       [0]      non-sarc
4                  [punct1]          sarc

通过评论编辑:

df = pd.DataFrame({'comment':[['?', '?'],[0], ['!', '!', '!'], [0], ['?']], 'sarc_majority': [ 'sarc','non-sarc', 'sarc', 'non-sarc','sarc']})
print (df)
     comment sarc_majority
0     [?, ?]          sarc
1        [0]      non-sarc
2  [!, !, !]          sarc
3        [0]      non-sarc
4        [?]          sarc

print (type(df.ix[0,'comment']))
<class 'list'>

replace_df = pd.DataFrame({'replacer': {0: 'punct1', 1: 'punct2', 2: 'punct3'}, 'punct': {0: '?', 1: '!', 2: "'"}})
#print (replace_df)

replace_df.punct = '\\' + replace_df.punct.apply(lambda x: x.format())
d = replace_df.set_index('punct')['replacer'].to_dict()
print (d)
{'\\!': 'punct2', "\\'": 'punct3', '\\?': 'punct1'}

df.comment = df.comment.apply(lambda x: pd.Series(x).astype(str).replace(d, regex=True).tolist())
print (df)
                    comment sarc_majority
0          [punct1, punct1]          sarc
1                       [0]      non-sarc
2  [punct2, punct2, punct2]          sarc
3                       [0]      non-sarc
4                  [punct1]          sarc

答案 1 :(得分:1)

大多数标点字符在正则表达式中具有特殊含义。在这里你最终会得到,例如:\b?\b,这意味着一个可选的边界,后跟一个边界。不是你的意思。

要将任意字符串传递到正则表达式,必须使用re.escape对其进行转义:

import re
r'\b{0}\b'.format(re.escape(punct))

这将是\b\?\b,表示边界,后跟?,后跟另一个边界。