Home > Software engineering >  count specific value over all dataframe in python
count specific value over all dataframe in python

Time:01-03

I have a large dataframe (235832 rows × 79 columns) that contains genotype data rows mean = variants columns mean = patients

I want to search many values in a dataframe ( all, not specific column or row )

So ,

I want to return the number of finding [-1, -1] or [0 -1] across all dataframe how can I do it in python

example of dataframe

0 1 2 3 ... 78

0 [-1, -1] [0, 0] [0, 0] ... [0 -1]

1 [0 0] [0,0] [-1 -1] ... [0 -1]

and so on until 235832 

I want count [-1,-1] or [0,-1] in the dataframe it return 4 in my example

CodePudding user response:

IIUC use:

test = [0,-1]
out = (df.stack().map(tuple) == tuple(test)).sum()
print (out)

CodePudding user response:

OK. I think you just need to count the occurrences of [-1, -1] and [0, -1]. I am not sure about the formatting. But in general,

Suppose, your dataframe's name is df

df_melted = df.melt()

Running the cell above will get all the values of your original dataframe in ONE column, then, you can run

df['value'].value_counts()

This will return you the counts of all the values within including what you are seeking.

  • Related