I have a dataframe and want to determine for a given column if every value in the column is equal to zero.
This is the code I have:
z <- read.zoo(sub, sep = ",", header = TRUE, index = 1:2, tz = "", format = "%Y-%m-%d %H:%M:%S")
if(all.equal(z$C_duration, 0))
C_dur_acf = NA
But I am getting an error:
Error in if (all.equal(z$C_duration, 0)) { :
argument is not interpretable as logical
The code should return a boolean value (TRUE/FALSE) if the entire column is all zeros.
Select the Dataframe column by its name i.e., df['C']. Then apply a condition on it i.e. ( df['C']==0 ). It gives a bool Series object, where each True value indicates that the corresponding value in the column is zero.
Pandas DataFrame all() Method The all() method returns one value for each column, True if ALL values in that column are True, otherwise False. By specifying the column axis ( axis='columns' ), the all() method returns True if ALL values in that axis are True.
Use all
builtin: all(z$C_duration == 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With