简体   繁体   English

使用正则表达式在不同列的熊猫数据框中查找单词并创建新值

[英]Find words and create new value in different column pandas dataframe with regex

suppose I have a dataframe which contains:假设我有一个包含以下内容的数据框:

df = pd.DataFrame({'Name':['John', 'Alice', 'Peter', 'Sue'],
                   'Job': ['Dentist', 'Blogger', 'Cook', 'Cook'], 
                  'Sector': ['Health', 'Entertainment', '', '']})

and I want to find all 'cooks', whether in capital letters or not and assign them to the column 'Sector' with a value called 'gastronomy', how do I do that?我想找到所有“厨师”,无论是否为大写字母,并将它们分配给名为“美食”的值的“部门”列,我该怎么做? And without overwriting the other entries in the column 'Sector'?并且不覆盖“部门”列中的其他条目? Thanks!谢谢!

Here's one approach:这是一种方法:

df.loc[df.Job.str.lower().eq('cook'), 'Sector'] = 'gastronomy'

print(df)

    Name      Job         Sector
0   John  Dentist         Health
1  Alice  Blogger  Entertainment
2  Peter     Cook     gastronomy
3    Sue     Cook     gastronomy

Using Series.str.match with regex and a regex flag for not case sensitive ( ?i ):使用Series.str.matchregex和正则表达式标志不区分大小写( ?i ):

df.loc[df['Job'].str.match('(?i)cook'), 'Sector'] = 'gastronomy'

Output输出


    Name      Job         Sector
0  John   Dentist  Health       
1  Alice  Blogger  Entertainment
2  Peter  Cook     gastronomy   
3  Sue    Cook     gastronomy 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 dataframe 中查找值并在 pandas 的新列中添加先例列值 - find a value in a dataframe and add precedent column value in a new column in pandas 使用Regex条件在Pandas DataFrame中创建新列 - Use Regex condition to create a new column in a Pandas DataFrame 如何创建新的 pandas 列,其中列表值 ==df 索引但列表的长度与原始 dataframe 的长度不同 - how to create new pandas column where list value ==df index but list is a different length to orignal dataframe 根据最新的列创建一个新列,并在数据框上有一个值 - Pandas - Create a new Column based on the latest column with a value on a dataframe - Pandas 使用column及其值在pandas数据框中创建一个新列 - Create a new column in pandas dataframe using column and its value pandas Dataframe 用特定值划分列并用结果创建新列? - pandas Dataframe divide a column with a specific value and create new column with the result? 将单词标记化为熊猫数据框中的新列 - Tokenizing words into a new column in a pandas dataframe 根据来自不同列 Pandas Dataframe 的值创建列 - Create column based on value from different column Pandas Dataframe 在列中查找匹配值并创建另一列 pandas dataframe - Find matching value in column and create another column pandas dataframe 根据不同条件在Pandas dataframe中新建一列 - Create a new column in Pandas dataframe based on different conditions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM