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

Python 仅替换数据帧中列的第一个字符,python,string,python-3.x,pandas,dataframe,Python,String,Python 3.x,Pandas,Dataframe,我试图替换数据帧中每行语句的第一个出现的某些单词。然而,进入“1”位置将取代一切。为什么在替换中通过“1”不起作用?有不同的方法吗? 谢谢 首字母: 尝试: 所需的最终产出: 编程语言,包括Python,读起来不像人类。您需要告诉Python按空格分割。例如,通过: 不知道为什么另一个答案被删除了,它更加简洁,并且起到了作用。(对不起,我不记得是谁发布的。我尝试了答案,但效果不错,但有一定的局限性) df.some_text.str.replace(“^ur”,“Our”).str.replac

我试图替换数据帧中每行语句的第一个出现的某些单词。然而,进入“1”位置将取代一切。为什么在替换中通过“1”不起作用?有不同的方法吗? 谢谢

首字母: 尝试: 所需的最终产出:
编程语言,包括Python,读起来不像人类。您需要告诉Python按空格分割。例如,通过:


不知道为什么另一个答案被删除了,它更加简洁,并且起到了作用。(对不起,我不记得是谁发布的。我尝试了答案,但效果不错,但有一定的局限性)

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

然而,正如评论中指出的,这将替换以“
ur
”(“ursula”)或“
he
”(“helen”)开头的所有起始字符

更正后的代码为:
^
”表示行的开头&只应替换行开头的不完整单词。“
\s
”表示第一个单词后面有空格,因此它只与正确的单词匹配。

哦,是的。我错过了。。我确实介绍了多个以“他”开头的句子。感谢突出显示:)在下面的数据框上测试:df=pd.dataframe({'some_text':[“你的目标是今天完成购书”,“我们的目标是今天完成购书”,“救援正在进行中。他很快就会来”,“道路畅通…他要去图书馆”。]]@jpp..谢谢你指出。我现在更新了我的答案。谢谢你的澄清。对于这个案例,它工作得很好。当我应用到我的原始数据集时,它抱怨
ValueError:没有足够的值来解包(预期为2,得到0)
@sharp,错误告诉您的字符串中至少需要2个单词,如果您理解逻辑(
表示i,j in…
).逻辑可以调整,你可以尝试这样做,但这是另一个问题。啊,好吧。我认为我这边有一些数据问题。我会解决的。我相信这会奏效。但是对于我发布的上述案例。你的解决方案很好。非常感谢!我在这方面花了很多时间。
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
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  
df.some_text.str.replace('^ur\s','Our ').str.replace('^he\s','The ')