pandas删除DataFrame中任意字段等于'null'字符串的行

删除df中任意字段等于'null'字符串的行:

df=df.astype(str)#把df所有元素转为str类型
df=df[df['A'].isin(['null','NULL'])] #找出df的'A'列值为'null'或'NULL'(注意此处的null是字符串,不是空值)
df=df[~df['A'].isin(['null','NULL'])] #过滤掉A列为'null'或'NULL'的行,~表示取反

去掉任意一列为'null'值的行,目前只能想到用循环:

for col in list(df.columns):
df=df[~df[col].isin(['null','NULL'])]

去掉包含(而非等于)'null'字符串列的行:

df=df[~df['A'].str.contains('null')]
原文地址:https://www.cnblogs.com/aaronhoo/p/9685268.html