我有一个相当大的数据帧(大约15米行,7列),我想替换一些不具有正确形状的值。在
我尝试在整个数据帧中迭代,但是一个一个地更改值需要太长时间。 我也尝试过使用regex,但是如果字符串与pandas不匹配,我无法找到如何替换。在
我的数据帧列如下:1 : L8_P1_Local
2 : L8
3 : L8_P1_Local
4 : L8
5 : poste2
6 : poste6
7 : poste2
8 : Poste 2
9 : poste_6
编辑:有时poste2和poste6是不同的,比如poste2或posteu2或posteu2 这个正则表达式会匹配所有内容吗?[pP]奥斯特[\s]*[\u]*[0-9]
我要做的是在poste2或poste6前面的每一行上都有L8,这样它就会像L8_poste6
。我在一个名为numline
的变量中有'L8'字符串。在
编辑:因为答案在被接受的答案评论中,我会把它写在这里。在
text = numligne +'_\\1'
dataframe['row'] = dataframe['row'].str.replace('([pP]oste[ _]*[0-9])', text)
如果您想添加
L8
它不在那里,您可以让pandas这样做:因此,我假设您有一个DataFrame(比如df),其中有一个列(比如col)包含示例数据:
你可以:
^{pr2}$获得:
使用
pd.Series.str.replace
:输出:
^{pr2}$有多种方法可以在整个数据帧中实现这一点,包括(但可能不是最快的):
相关问题 更多 >
编程相关推荐