Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Get rows with same value from dataframe of particular columns
#1
I have a dataframe with a lot of columns with binary values.
İmage


Is it possible to count the number of rows that satisfy a condition

ex: the number of rows for Col1 and Col2 that are both 1 is 2

similarly the number of rows for Col1 and Col2 that are 0 and 1 is 1

Is there an easy way to do this rather than looping all over the dataframe?
Quote
#2
import pandas as pd
z = pd.DataFrame({'Col1': [1,0,1,1,0,0,1], 'Col2': [0,1,1,1,0,1,0]})

# The number of rows, where both Col1 and Col2 == 0
((z.Col1==0)&(z.Col2==0)).sum()

# The number of rows, where Col1==0 and Col2 == 1
((z.Col1==0)&(z.Col2==1)).sum()

# The number of rows, where both Col1 and Col2 == 1
((z.Col1==1)&(z.Col2==1)).sum()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Difference of two columns in Pandas dataframe zinho 0 92 Jun-01-2020, 01:11 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 221 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 245 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 238 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  How to highlight dataframe columns SriRajesh 1 240 Feb-28-2020, 11:02 PM
Last Post: Marbelous
  Does the order of columns in the DataFrame matter? new_to_python 11 541 Feb-17-2020, 12:32 AM
Last Post: scidam
  read_csv error and rows/columns missing karlito 9 592 Nov-11-2019, 06:48 AM
Last Post: karlito
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 583 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  How to add a few empty rows into a pandas dataframe python_newbie09 2 3,711 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Drop rows if a set of columns has a value dervast 1 360 Sep-12-2019, 04:18 PM
Last Post: sd_0912

Forum Jump:


Users browsing this thread: 1 Guest(s)