简体   繁体   English

根据条件从数据框中的现有列创建新列

[英]Creating a new column from existing column in the data frame based on the criteria

Create a new column based on the below criteria.根据以下条件创建一个新列。 Data Type for column A is String A 列的数据类型是字符串

If there exists a text in column A string.如果 A 列字符串中存在文本。 Create column B which contains only the string.创建仅包含字符串的 B 列。 eg: Column A: This product is used for pipefitter Desired: if column A contains pipefitter Column B = pipefitter例如: A 列:该产品用于管道安装器 期望:如果 A 列包含管道安装器 B 列 = 管道安装器

"""df["New_text"] = []
def update_text(selected_text):
    for text in df['Activity Name']:
        if selected_text in text:
            df['New_text'] = df['New_text'].append('text')"""

So if i understand correctly.所以如果我理解正确的话。 You have a column Activity Name in your dataframe and you want to write a method which would each time print a subset of a dataframe which has a particular keyword.您的数据框中有一列Activity Name ,并且您想编写一个方法,该方法每次都会打印具有特定关键字的数据框的子集。

If my understanding is right then.如果我的理解是对的。

def update_text(df, selected_text):
    return df[df["Activity Name"].str.contains(selected_text)]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM