[Solved] python progrom to find the null values in row and return the column name


You should edit your question so that it becomes clear to everyone, e.g. what format you used, what you have tried to get that output and so on.

Actually, it’s an interesting problem. I assume you have above data in pandas’ dataframe format. It’s also important to add more tags related to your question.

Anyway, I couldn’t find more effective way than below code. Hope it helps you:

nul_index = []
for i in df:
    nul_index.append(df[df[i].isnull()].index.tolist())

That will give you list of row indexes of null values column-wise. In your case above: [[],[1],[0,1],[1],[2],[]]

From that list we want something like [['C'],['B','C','D'],['E']] to assign it to our new column.

new_col = []
for i in range(len(df)):
    new_col.append([df.columns[x] for x in range(len(nul_index)) if i in nul_index[x]])
df['null_column'] = new_col

solved python progrom to find the null values in row and return the column name