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
- Loop over the 'indx1', 'indx2' columns of 'df' in
Map
- Extract the corresponding columns of 'df1' -
df1[[x]]
,df1[[y]]
- Create the multiple logical expression with
>
and&
- Check if there
any
TRUE
value from the rows of 'df1' - Coerce to binary (
+(
- or useas.integer
) - Convert the
list
output to avector
-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
How to Dynamically Build a Json Object
Replacing Pandas or Numpy Nan With a None to Use With Mysqldb
Pip Install Pandas: Installing Dependencies Error
Pandas - Drop Last Column of Dataframe
Populating a List in Python Using for Loop
Django Rest Framework Csrf Failed: Csrf Cookie Not Set
How to Convert an Integer to Time
Python Json Serialize a Decimal Object
How to Send Smtp Email for Office365 With Python Using Tls/Ssl
How to Write Multiple Images (Subplots) into One Image
Reading Columns of a Txt File on Python
Python | Make the Percentage of a List
Conditional Row Read of CSV in Pandas
Python: Getting Around Division by Zero
Import Error: Dll Load Failed in Jupyter Notebook But Working in .Py File
How to Display the Value of the Bar on Each Bar With Pyplot.Barh()