简体   繁体   English

如果 DataFrame 包含特定字符串,则创建新列

[英]Create new column if DataFrame contains specific string

I have one column in the DataFrame that is a name.我在DataFrame中有一个column是一个名称。 Inside this name there are patterns that I want to locate, and create a category in other column of same DataFrame .在这个名称中,有我想要定位的模式,并在同一DataFrame其他column中创建一个类别。 For example :例如 :

Name 

name first RB LA a 
name LB second
RB name third
name LB fourth 

I want the name with the same pattern to be in the same category, displayed in the other column我希望具有相同模式的名称在同一类别中,显示在另一列中

What I want :我想要的是 :

       Name                  Example          

name first RB LA a          Round Blade category
name LB second              Long Biased category
RB name third               Round Blade category
name LB fourth              Long Biased category

I have a DataFrame , not a list, there are several other columns in it.我有一个DataFrame ,而不是一个列表,其中还有其他几个列。 And there are not only two categories, but several ones.而且不仅有两个类别,而且有几个类别。

What I have Tried :我试过的:

df.loc[df['Name']=="RB", 'Example'] = "RB category"

But it does not work since it must be an exact match但它不起作用,因为它必须完全匹配

Another attempt :另一种尝试:

if df[['Name'].str.contains("RB")] : 
    (...)

But it gives me error :但它给了我错误:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried to add to .bool() or .any() , but or the error persist or nothing happens when I run the line.我试图添加到.bool().any() ,但是当我运行该行时,错误仍然存​​在或没有任何反应。

Thank you.谢谢你。

You could use pandas.Series.str.extract to achieve the desired output您可以使用pandas.Series.str.extract来实现所需的输出


import numpy as np
import pandas as pd


df = pd.DataFrame({
    "Name": ["name first RB LA a", "name LB second", "RB name third", "name LB fourth"]
})
df["Example"] = df["Name"].str.extract("(LB|RB)")[0] + " category"

    Name                Example
0   name first RB LA a  RB category
1   name LB second      LB category
2   RB name third       RB category
3   name LB fourth      LB category

Edit编辑

To change category names within Example column use .str.replace :要更改Example列中的类别名称,请使用.str.replace

df["Example"] = (df["Example"]
 .str.replace("RB", "Round Blade")
 .str.replace("LB", "Long Biased")
)

暂无
暂无

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

相关问题 如果一行包含特定字符串,pandas 是否可以在新列中创建一个整数 - Is there a way in pandas to create an integer in a new column if a row contains a specific string 如果该列包含另一个数据框的列中的字符串,则在该数据框中创建一个新列 - Create a new column in a dataframe if the column contains a string from a column of another dataframe 如果现有列的值包含特定子字符串,则创建新的 pd.DataFrame 列 - Create new pd.DataFrame column if value of existing column contains specific substring 数据框列是一个包含国家/地区的字符串,我想用该国家/地区创建一个新列 - Dataframe column is a string that contains a country, I want to create a new column with that country 从数据框的两列创建一个新列,其中每列的行包含字符串格式的列表 - Create a new column from two columns of a dataframe where rows of each column contains list in string format dataframe 复制行到一个新的 dataframe 如果它包含一个特定的字符串 - dataframe copy row to a new dataframe if it contains a specific string 如果字符串“包含”子字符串,则创建一个带有条件的新列? - Create a new column with condition if a string 'contains' substring? 根据另一列中字符串的特定字符 pandas 创建新的 dataframe 列 - Create new dataframe column based on a specific character of a string in another column, pandas 如果第二列中的文本包含特定字符串模式,那么如果一列中的文本创建新列? - How do I create a new column if the text from one column if the text from a second column contains a specific string pattern? 检查字符串以在火花数据框中创建新列 - Inspect a string to create a new column in spark dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM