仅替换数据框中列的第一个字符

时间:2018-12-03 16:23:02

标签: python string python-3.x pandas dataframe

我正在尝试替换数据帧中每一行中语句开头的某些单词。但是,传递“ 1”位置将替换所有内容。为什么在替换中传递“ 1”不起作用?有不同的方法吗? 谢谢!

初始:

df_test = pd.read_excel('sample.xlsx')
print('Initial: \n',df_test)

Initial: 
                                         some_text
0   ur goal is to finish shopping for books today
1  Our goal is to finish shopping for books today
2                          The help is on the way
3        he way is clear … he is going to library

尝试:

df_test['some_text'] = df_test['some_text'] \
        .str.replace('ur ','Our ',1) \
        .str.replace('he ','The ',1) 
print('Tried:\n',df_test)

Tried: (Incorrect Results) 
                                          some_text
0   Our goal is to finish shopping for books today
1  OOur goal is to finish shopping for books today
2                          TThe help is on the way
3        The way is clear … he is going to library

所需的最终输出:

                                    some_text
0   Our goal is to finish shopping for books today
1  Our goal is to finish shopping for books today
2                          The help is on the way
3        The way is clear … he is going to library

2 个答案:

答案 0 :(得分:3)

不知道为什么其他答案被删除了,简明扼要地完成了工作。 (对不起,我不记得是谁发布的。我尝试了这个答案,虽然可以,但是有一定限制)

df.some_text.str.replace('^ur','Our ').str.replace('^he','The ')

但是,正如注释中指出的那样,它将替换所有以'ur'('ursula')或'he'('helen')开头的起始字符。

更正后的代码为:

df.some_text.str.replace('^ur\s','Our ').str.replace('^he\s','The ')

^”表示行首,并且只能替换行首中不完整的单词。 “ \s”表示第一个单词之后的空格,因此仅与正确的单词匹配。

答案 1 :(得分:2)

包括Python在内的编程语言读起来并不像人类。您需要告诉Python按空格分割。例如,通过str.split

df = pd.DataFrame({'some_text': ['ur goal is to finish shopping for books today',
                                 'Our goal is to finish shopping for books today',
                                 'The help is on the way',
                                 'he way is clear … he is going to library']})

d = {'ur': 'Our', 'he': 'The'}

df['result'] = [' '.join((d.get(i, i), j)) for i, j in df['some_text'].str.split(n=1)]

print(df)

                                        some_text  \
0   ur goal is to finish shopping for books today   
1  Our goal is to finish shopping for books today   
2                          The help is on the way   
3        he way is clear … he is going to library   

                                           result  
0  Our goal is to finish shopping for books today  
1  Our goal is to finish shopping for books today  
2                          The help is on the way  
3       The way is clear … he is going to library