Check If Dataframe Has a Zero Element

Check if dataframe has a zero element

Maybe using any twice

df.eq(0).any().any()
Out[173]: True

How to find if if a particular column has zero value in a dataframe?

As the comment fros mosc9576 said, you can apply the same syntax you had int the .all()

if (df['bp_move'] == 0).all():
print('all')
if (df['bp_move'] == 0).any():
print('any')

Check if pandas column contains all zeros

you can do something like this:

(df['col'] == 0).all()

This will return True if all values are 0 otherwise it will return false

How to check whether a pandas DataFrame is empty?

You can use the attribute df.empty to check whether it's empty or not:

if df.empty:
print('DataFrame is empty!')

Source: Pandas Documentation

Is there a way to check if dataframe is empty and if so to add a NA row?

Simple question, OP, but actually pretty interesting. All the elements of your code should work, but the issue is that when you run as is, it will return a list, not a data frame. Let me show you with an example:

growing_df <- data.frame(
A=rep(1, 3),
B=1:3,
c=LETTERS[4:6])

df_empty <- data.frame()

If we evaluate as you have written you get:

df <- ifelse(dim(df_empty)[1]==0, rbind(growing_df, NA))

with df resulting in a List:

> class(df)
[1] "list"
> df
[[1]]
[1] 1 1 1 NA

The code "worked", but the resulting class of df is wrong. It's odd because this works:

> rbind(growing_df, NA)
A B c
1 1 1 D
2 1 2 E
3 1 3 F
4 NA NA <NA>

The answer is to use if and else, rather than ifelse(), just as @akrun noted in their answer. The reason is found if you dig into the documentation of ifelse():

ifelse returns a value with the same shape as test which is filled
with elements selected from either yes or no depending on whether the
element of test is TRUE or FALSE.

Since dim(df_empty)[1] and/or nrow(df_empty) are both vectors, the result will be saved as a list. That's why if {} works, but not ifelse() here. rbind() results in a data frame normally, but the class of the result stored into df when assigning with ifelse() is decided based on the test element, not the resulting element. Compare that to if{} statements, which have a result element decided based on whatever expression is input into {}.

Check if columns of one data frame are present in another data frame with non-zero element in R

We can use Map

  1. Loop over the 'indx1', 'indx2' columns of 'df' in Map
  2. Extract the corresponding columns of 'df1' - df1[[x]], df1[[y]]
  3. Create the multiple logical expression with > and &
  4. Check if there any TRUE value from the rows of 'df1'
  5. Coerce to binary (+( - or use as.integer)
  6. Convert the list output to a vector - unlist and assign it to create the 'count_occ' column in 'df'
df$count_occ <- unlist(Map(function(x, y) 
+(any(df1[[x]] > 0 & df1[[y]] > 0, na.rm = TRUE)), df$indx1, df$indx2))

-output

df
indx1 indx2 count_occ
1 aa 1 ac 0
2 ac tg 0 1

data

df <- structure(list(indx1 = c("aa 1", "ac"), indx2 = c("ac", "tg 0"
)), class = "data.frame", row.names = c(NA, -2L))

df1 <- structure(list(col1 = c("A", "B", "C", "D", "E"), `aa 1` = c(1L,
0L, 1L, 0L, 0L), `ab 2` = c(0L, 0L, 1L, 0L, 0L), ac = c(0L, 1L,
0L, 0L, 1L), `bd 5` = c(1L, 1L, 1L, 5L, 0L), `tg 0` = c(4L, 0L,
1L, 5L, 9L)), class = "data.frame", row.names = c(NA, -5L))


Related Topics



Leave a reply



Submit