[英]Creating a new column in a data frame by mapping the values from a dictionary to an existing column
[英]Creating a new column from existing column in the data frame based on the criteria
根據以下條件創建一個新列。 A 列的數據類型是字符串
如果 A 列字符串中存在文本。 創建僅包含字符串的 B 列。 例如: 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')"""
所以如果我理解正確的話。 您的數據框中有一列Activity Name
,並且您想編寫一個方法,該方法每次都會打印具有特定關鍵字的數據框的子集。
如果我的理解是對的。
def update_text(df, selected_text):
return df[df["Activity Name"].str.contains(selected_text)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.