为什么替换功能不能更改列文本?

时间:2019-10-01 19:21:15

标签: python pandas dataframe

我有一个读取CSV文件并返回True或False的程序。现在,我想使用If语句用字符串替换列文本,但是replace函数不起作用。

import pandas as pd

data = pd.read_excel(r'C:/Users/bryanmccormack/Downloads/***.xlsx')
data = data.astype(str)
df = pd.DataFrame(data, columns = ["Date Added","Track 
Item","Retailer","Retailer Item ID","TLD","UPC","Title",
                              "Manufacturer","Brand","Client Product 
Group","Category","Subcategory",
                              "Amazon Sub Category","Segment","Platform"])

excludes = ["chainsaw","pail","leaf","blower","genie", 
"straightener","backpack","iron","curling","dog","cat","wig"]

my_excludes = [set(key_word.lower().split()) for key_word in excludes]
match_titles = [e for e in df.Title if any(keywords.issubset for keywords 
in my_excludes)]

def is_match(title, excludes=excludes):
     my_excludes = [set(key_word.lower().split()) for key_word in 
     excludes]
     if any(keywords.issubset(title.lower().split()) for keywords in 
    my_excludes):
      return True
  return False
df['match_titles'] = df['Title'].apply(is_match)
result = df[df['match_titles']]['Retailer Item ID']

if df['match_titles'] == "True":
df['Track Items'].replace("Z-Exclude")

如果标题包含我建立的关键字,则代码会将列返回为True或False,但这是我想要的,但是***之后的代码不起作用。如果“匹配标题”列为“真”,我希望“跟踪项”列显示“ Z-排除”。为什么不将列文本从“跟踪项目”更改为“ Z排除”?

1 个答案:

答案 0 :(得分:0)

尝试一下:

SQLite version 3.29.0 2019-07-10 17:32:03
Enter ".help" for usage hints.
sqlite> .dbinfo
the ".dbinfo" command requires the -DSQLITE_ENABLE_DBPAGE_VTAB compile-time options
sqlite> .quit