【发布时间】:2021-04-26 15:46:42
【问题描述】:
我创建了一个函数,其输入是 pandas 数据框。
它应该返回具有缺失值的行的行索引。
它适用于所有定义的 Missingness 值,除非单元格完全为空 - 即使我尝试在 missing_values 列表中将其指定为 [...,""] 。
这可能是什么问题?或者有没有更直观的方法来解决这个问题?
def missing_values(x):
df=x
missing_values = ["NaN","NAN","NA","Na","n/a", "na", "--","-"," ","","None","0","-inf"] #common ways to indicate missingness
observations = df.shape[0] # Gives number of observations (rows)
variables = df.shape[1] # Gives number of variables (columns)
row_index_list = []
#this goes through each observation in the first row
for n in range(0,variables): #this iterates over all variables
column_list = [] #creates a list for each value per variable
for i in range(0,observations): #now this iterates over every observation per variable
column_list.append(df.iloc[i,n]) #and adds the values to the list
for i in range(0,len(column_list)): #now for every value
if column_list[i] in missing_values: #it is checked, whether the value is a Missing one
row_index_list.append(column_list.index(column_list[i])) #and if yes, the row index is appended
finished = list(set(row_index_list)) #set is used to make sure the index only appears once if there are multiple occurences in one row and then it is listed
return finished
【问题讨论】:
标签: python pandas missing-data