Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to check multiple columns with the same condition in pandas?

Tags:

python

pandas

I got the output but trying to find a more efficient way to do this:

(df['budget'] == 0).sum(), (df['revenue'] == 0).sum(),(df['budget_adj'] == 0).sum(), (df['revenue_adj'] == 0).sum()

Output is

(5674, 5993, 5676, 5993)
like image 930
Ankvis Avatar asked Mar 07 '20 20:03

Ankvis


People also ask

How do I use multiple conditions in pandas?

Using Loc to Filter With Multiple Conditions The loc function in pandas can be used to access groups of rows or columns by label. Add each condition you want to be included in the filtered result and concatenate them with the & operator. You'll see our code sample will return a pd. dataframe of our filtered rows.

How to compare two columns in a pandas Dataframe?

Here are a few things to keep in mind when comparing two columns in a pandas DataFrame: The number of conditions and choices should be equal. The default value specifies the value to display in the new column if none of the conditions are met. Both NumPy and Pandas are required to make this code work.

Why do we need to filter pandas Dataframe with multiple conditions?

The reason is dataframe may be having multiple columns and multiple rows. Selective display of columns with limited rows is always the expected view of users. To fulfill the user’s expectations and also help in machine deep learning scenarios, filtering of Pandas dataframe with multiple conditions is much necessary.

How to compare the number of goals in a pandas Dataframe?

We can use the following code to compare the number of goals by row and output the winner of the match in a third column: The results of the comparison are shown in the new column called winner. Here are a few things to keep in mind when comparing two columns in a pandas DataFrame: The number of conditions and choices should be equal.

How to check if two columns are equal(identical) with pandas?

How to check if two columns are equal (identical) with pandas ? To check if two columns are equal a solution is to use pandas.DataFrame.equals, example: here we added a column called diff (for difference) where 1 means same value in " Score A " and " Score B" else 0.


1 Answers

You can compare the columns in bulk and sum these up column-wise:

(df[['budget', 'revenue', 'budget_adj', 'revenue_adj']] == 0).sum(axis=0)
like image 188
Willem Van Onsem Avatar answered Oct 26 '22 12:10

Willem Van Onsem