如果在熊猫中包含某些特定字符,请用另一个单词替换

时间:2019-11-11 03:15:25

标签: python-3.x pandas

如果熊猫数据框中的某些字符包含某些字符,我想替换它们。

pd.Series(['first mashmalowss ', 'second', 'third row', 'fourth powwww']) 

我想将包含“ ow”的词替换为“值”一词。预期输出:

pd.Series(['first value ', 'second', 'third value', 'fourth value']) 

实际上,第1、3和第4行的值带有一个词,其字符“ ow”

2 个答案:

答案 0 :(得分:1)

使用apply方法和辅助函数:

import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.array(['first mashmalowss ', 'second', 'third row', 'fourth powwww']).T,
                  columns=['heading'])

def replace(string: str) -> str:
    if 'ow' in string:
        rv = ''
        for word in string.split():
            rv += ' value' if 'ow' in word else ' ' + word
        return rv.strip()
    else:
        return string

print (df)
print(df['heading'].apply(replace))

函数replace接受系列中的任何元素,并将其清理为规范。 df['heading'].apply(replace)行将此功能应用于您系列中的每个值。

答案 1 :(得分:1)

系列replace应该起作用

s = pd.Series(['first mashmalowss ', 'second', 'third row', 'fourth powwww'])

s1 = s.replace(r'\w*ow\w*', 'value', regex=True)
print(s1)

Out[34]:
0    first value
1          second
2     third value
3    fourth value
dtype: object