如果我有一个看起来像这样的 csv,第一个 A、B、C 是列标题:
A, B, C
A1,A2,A3
blah,B2,B3
C1,C2,C3
blahxxxxxtr,D4,D5
如何删除包含 'blah' 或任何包含 'blah' 的条目,而不删除整行。
这是目前的工作:
import pandas as pd
file = r"\\fileserver\data\test.csv"
df = pd.read_csv(file)
for index, row in df.iterrows():
if 'blah' in str[row[0]:
print(row['A']
#this is where I don't know how to remove 'blah' if this is True
#I want the new value of that to be a blank field, so '',B2,B3
#Same would go for the 4th row, '',D4,D5
# Using a drop command removes the whole row.
df.to_csv(file, index = false)
这成功打印了 'blah' 值的第 2 行和第 4 行。
我如何删除并用一个空白字符串 '' 替换 'blah',这样它就没有任何东西,甚至在该特定行的该列中没有 Nan?
答案 0 :(得分:1)
您可以简单地使用替换。
df.replace(to_replace='blah', value='the value you want', inplace=True)
或者如果你想替换某个列值使用
df[colname].replace(to_replace='blah', value='the value you want', inplace=True)
如果 inplace 设置为 true,df 将在内部更新。否则将返回更改值的 df