Python 条件 If 语句:如果行中的值包含字符串...设置另一列等于字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43905930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:30:50  来源:igfitidea点击:

Conditional If Statement: If value in row contains string ... set another column equal to string

pythonstringif-statementconditionalcontains

提问by PineNuts0

EDIT MADE:

编辑制作:

I have the 'Activity' column filled with strings and I want to derive the values in the 'Activity_2' column using an if statement.

我有一个充满字符串的“Activity”列,我想使用 if 语句导出“Activity_2”列中的值。

So Activity_2 shows the desired result. Essentially I want to call out what type of activity is occurring.

因此 Activity_2 显示了所需的结果。本质上,我想指出正在发生的活动类型。

I tried to do this using my code below but it won't run (please see screen shot below for error). Any help is greatly appreciated!

我尝试使用下面的代码执行此操作,但它无法运行(有关错误,请参阅下面的屏幕截图)。任何帮助是极大的赞赏!

enter image description here

在此处输入图片说明

    for i in df2['Activity']:
        if i contains 'email':
            df2['Activity_2'] = 'email'
        elif i contains 'conference'
            df2['Activity_2'] = 'conference'
        elif i contains 'call'
            df2['Activity_2'] = 'call'
        else:
            df2['Activity_2'] = 'task'


Error: if i contains 'email':
                ^
SyntaxError: invalid syntax

采纳答案by DovaX

The current solution behaves wrongly if your df contains NaN values. In that case I recommend using the following code which worked for me

如果您的 df 包含 NaN 值,则当前解决方案的行为是错误的。在这种情况下,我建议使用以下对我有用的代码

temp=df.Activity.fillna("0")
df['Activity_2'] = pd.np.where(temp.str.contains("0"),"None",
                   pd.np.where(temp.str.contains("email"), "email",
                   pd.np.where(temp.str.contains("conference"), "conference",
                   pd.np.where(temp.str.contains("call"), "call", "task"))))

回答by Psidom

I assume you are using pandas, then you can use numpy.where, which is a vectorized version of if/else, with the condition constructed by str.contains:

我假设您正在使用pandas,那么您可以使用numpy.where,它是if/else的矢量化版本,条件由str.contains

df['Activity_2'] = pd.np.where(df.Activity.str.contains("email"), "email",
                   pd.np.where(df.Activity.str.contains("conference"), "conference",
                   pd.np.where(df.Activity.str.contains("call"), "call", "task")))

df

#   Activity            Activity_2
#0  email personA       email
#1  attend conference   conference
#2  send email          email
#3  call Sam            call
#4  random text         task
#5  random text         task
#6  lwantto call        call

回答by moshfiqur

This also works:

这也有效:

df.loc[df['Activity'].str.contains('email'), 'Activity_2'] = 'email'
df.loc[df['Activity'].str.contains('conference'), 'Activity_2'] = 'conference'
df.loc[df['Activity'].str.contains('call'), 'Activity_2'] = 'call'

回答by Prakash Palnati

you have an invalid syntax for checking strings.

检查字符串的语法无效。

try using

尝试使用

 for i in df2['Activity']:
        if 'email' in i :
            df2['Activity_2'] = 'email'