【发布时间】:2021-04-01 18:01:54
【问题描述】:
如果我有一个像这样的 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)
这成功打印了第 2 行和第 4 行 'blah' 值。
如何删除并用空白字符串替换 'blah',所以它没有任何内容,甚至在该特定行的该列中也没有 Nan?
【问题讨论】:
-
试试
df.replace('blah', '')。