Python 基于另一个字段中的字符串值,使用文本在数据框中创建新字段
下面是我的python数据帧表。我想要的结果在突出显示的黄色列中 下面是我要实现的代码逻辑:Python 基于另一个字段中的字符串值,使用文本在数据框中创建新字段,python,string,dataframe,text,field,Python,String,Dataframe,Text,Field,下面是我的python数据帧表。我想要的结果在突出显示的黄色列中 下面是我要实现的代码逻辑: 如果“奖励”列包含“顶级IRA顾问”,那么我希望“行业认可标志”字段显示“认可为顶级IRA顾问”。否则,我希望它是空白的 下面是我尝试过但不起作用的代码: df_rfholder['Industry_Recognition_Flag'] = np.where(df_rfholder['Award'].str.contains('(?:Top IRA Advisor)', regex = True)
- 如果“奖励”列包含“顶级IRA顾问”,那么我希望“行业认可标志”字段显示“认可为顶级IRA顾问”。否则,我希望它是空白的李>
df_rfholder['Industry_Recognition_Flag'] = np.where(df_rfholder['Award'].str.contains('(?:Top IRA Advisor)', regex = True), 'Recognized as Top IRA Advisor', '')
非常感谢您的帮助
您可以使用.str.match()。。。
以下是一个工作示例:
import datetime
import pandas as pd
import numpy as np
d = {'one' : pd.Series(['','2016 Top IRA Advisor','2016 Top IRA Advisor'], index=['a', 'b', 'c']), 'two' : pd.Series(['Recognized', 'Recognized', 'Recognized'], index=['a', 'b', 'c'])}
df = pd.DataFrame(d)
df["new"] = np.where(df['one'].str.match('.*Top IRA Advisor'), 'true', 'false')
print(df)
这么简单
>>> import pandas as pd
>>> data = {'Award': 8*['']+['2016 Top IRA Advisor', '', '2016 Top IRA Advisor']}
>>> df = pd.DataFrame(data)
>>> df
Award
0
1
2
3
4
5
6
7
8 2016 Top IRA Advisor
9
10 2016 Top IRA Advisor
>>> df['Desired Result']=df['Award'].apply(lambda x: 'Recognized as Top IRA Advisor' if x=='2016 Top IRA Advisor' else '')
>>> df
Award Desired Result
0
1
2
3
4
5
6
7
8 2016 Top IRA Advisor Recognized as Top IRA Advisor
9
10 2016 Top IRA Advisor Recognized as Top IRA Advisor
谢谢回复。不幸的是,它不起作用。。。每一行都返回为'true'@PineNuts0奇怪,因为我使用了一个独立的示例,并且在前面使用了.match()。我重新阅读了您的示例,更改正则表达式代码对我也很有用。。。df_rfholder['Industry_Recognition_Flag']=np。其中(df_rfholder['Award'].str.包含('.'顶级IRA顾问',regex=True),'公认为顶级IRA顾问','')thx作为响应;我的问题是,即使在“奖励”字段中的值没有显示顶级IRA顾问,行业认可标志字段仍然显示“认可为顶级IRA顾问”