【发布时间】:2018-12-29 17:15:27
【问题描述】:
我有一个这样的数据框
df:
col1 col2
blue water bottle blue
red wine glass red
green cup green
我想创建另一列,它将忽略来自col1 的col2 的值
例如,新列 col3 将是:
water bottle
wine glass
green cup
我试过这段代码:
df.apply(lambda x: x['col1'].replace(x['col2'], ''), axis=1)
但我收到以下错误:
AttributeError: ("'NoneType' 对象没有属性 'replace'", '发生在索引 0')
怎么做?
【问题讨论】:
-
df["col3"] = df.apply(lambda x: x["col1"].replace(x["col2"], ""), axis=1)应该可以工作。我猜你在某处有None值。你可以检查df[df.col1.isnull()]