Replace whole string if it contains substring in pandas

后端 未结 4 2248
感情败类
感情败类 2020-11-27 04:46

I want to replace all strings that contain a specific substring. So for example if I have this dataframe:

import pandas as pd
df = pd.DataFrame({\'name\': [\         


        
4条回答
  •  忘掉有多难
    2020-11-27 05:15

    You can use apply with a lambda. The x parameter of the lambda function will be each value in the 'sport' column:

    df.sport = df.sport.apply(lambda x: 'ball sport' if 'ball' in x else x)
    

提交回复
热议问题