我正在尝试从下面的数据框中删除所有单词not
。
d = {'keep': ["not useful", "useful", "not useful", "useful", "useful"]}
df = pd.DataFrame(data=d)
df['keep'] = df['keep'].replace('not', '', inplace=True)
df
这导致我的“保持”列等于None
。
不知道我在这里做错了什么。
谢谢
答案 0 :(得分:2)
两个问题:
inplace=True
执行 inplace 操作,因此它直接修改序列并返回None
。df['keep'].replace(a,b)
替换整个字符串,而不是子字符串。您要
df['keep'] = df['keep'].replace('not','', regex=True)
答案 1 :(得分:0)
使用:
for i in range(len(df['keep'])):
df['keep'][i] = df['keep'][i].replace('not', '')