Skip to content
Advertisement

Identify the columns which contain zero and output its location

Suppose I have a dataframe where some columns contain a zero value as one of their elements (or potentially more than one zero). I don’t specifically want to retrieve these columns or discard them (I know how to do that) – I just want to locate these. For instance: if there is are zeros somewhere in the 4th, 6th and the 23rd columns, I want a list with the output [4,6,23].

Advertisement

Answer

You could iterate over the columns, checking whether 0 occurs in each columns values:

[i for i, c in enumerate(df.columns) if 0 in df[c].values]
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement