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

标签 python pandas

我的数据样本是:

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

我想用新名称替换标点符号。例如 ? = 点1,! = punct2,' = punct3。我尝试使用从 csv 文件读取。

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

提前致谢。干杯。

最佳答案

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

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

您还可以从 replace_df 创建 d:

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

关于python - 如何用新名称替换特定标点符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40627467/

相关文章:

python - 如何将这个函数向量化?

Python - 将 24 个字符的人类可读日期格式(如 'Tue May 16 04:10:55 PDT 2017')转换为 yyyy-mm-dd?

python - 根据另一列的日期和类别创建数据排名

python - 从函数的输出添加新列到 Pandas Dataframe

python - pyspark 在分组的 applyInPandas 中添加多列(更改架构)

python - 如何从 Python 中的图像中删除渐晕效果?

python - 使用 eval 在全局范围内创建新变量

Python 安装卸载 easy_install

python - 在 Pandas 中的字符串中解压列表中的字典

python - Pandas read_csv usecols 相同的索引