This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How To Process Array In Python As In R

Dear all,

I have an array, namely A, (m rows and n columns), I would like to calculate "sum of a column if criteria in other columns"

Example: sum of the 2nd column and criteria in the first column

In R:

sum(A[A[, 1] < h, 2])

How I can do it in python without loops?

Thanks,

python array r

this is a basic programming question, should be asked on stackoverflow.

5 answers

So the R code you posted will sum up values in column 2 that have a corresponding column 1 value less than 'h'? Let's say your python structure is an array of arrays:

rows = [[0,6],[1,8],[0,2],[1,5]]

Representing the following table of two columns:

0 6
1 8
0 2
1 5

You want to add up values in column 2 given the criteria that the corresponding column 1 is bigger than 0. This sum would be 8 + 5 = 13.

You can do something like this (not strictly without loops, but a small one liner):

sum([col[1] for col in rows if col[0] > 0])

If you need to work with tabular data in python, I can suggest you at least two libraries:

  • pandas introduces an object similar to the data.frame in R, and allows for fast indexing and subsetting
  • numpy is the most known library for manipulating arrays and matrices. It is inspired to Matlab and not to R, so it may be a little more difficult for you to learn it; but it should work quite well.

Examples

contents of mydata.txt:

"Date","sulfate","nitrate","ID"
"2008-07-05",4.58,0.353,100
"2008-07-11",1.54,0.101,100
"2008-07-17",7.73,0.111,100
"2008-07-23",2.14,0.356,100
"2008-07-29",4.63,0.0886,100
"2008-08-04",3.26,0.0783,100
"2008-08-16",4.35,0.112,100
"2008-08-22",6.28,0.326,100
"2008-08-28",4.06,0.261,100
"2008-09-03",11.1,0.0879,100
"2008-09-09",5.69,0.592,100
"2008-09-21",6.98,0.166,100
"2008-09-27",3.83,0.364,100
"2008-10-03",2.22,0.448,100

Example of pandas usage:

>>> mydata = pandas.read_csv("data.csv")
>>> print mydata
          Date  sulfate  nitrate   ID
0   2008-07-05     4.58   0.3530  100
1   2008-07-11     1.54   0.1010  100
2   2008-07-17     7.73   0.1110  100
3   2008-07-23     2.14   0.3560  100
4   2008-07-29     4.63   0.0886  100
5   2008-08-04     3.26   0.0783  100
6   2008-08-16     4.35   0.1120  100
7   2008-08-22     6.28   0.3260  100
8   2008-08-28     4.06   0.2610  100
9   2008-09-03    11.10   0.0879  100
10  2008-09-09     5.69   0.5920  100
11  2008-09-21     6.98   0.1660  100
12  2008-09-27     3.83   0.3640  100
13  2008-10-03     2.22   0.4480  100

# applying a selection criteria to one column
>>> mydata[mydata["sulfate"] > 4]
          Date  sulfate  nitrate   ID
0   2008-07-05     4.58   0.3530  100
2   2008-07-17     7.73   0.1110  100
4   2008-07-29     4.63   0.0886  100
6   2008-08-16     4.35   0.1120  100
7   2008-08-22     6.28   0.3260  100
8   2008-08-28     4.06   0.2610  100
9   2008-09-03    11.10   0.0879  100
10  2008-09-09     5.69   0.5920  100
11  2008-09-21     6.98   0.1660  100

# getting the sum of a column after applying a filter:
>>> sum(mydata[mydata["sulfate"] > 4]["sulfate"])
55.399999999999991

Moreover, in a comment you said:

I am going to use Python because I hope it's faster than R

Sorry but this is not a very good strategy. You will get better results if you concentrate on one programming language, and try to find if somebody else has found solutions to improve its efficiency. For example, have you had a look at the data.table library in R?

Python won't be faster than R just because it's generically faster, and I fear that it may be even slower for handling datasets. You can get better performance by improving your code or using a library designed for the task, rather than by switching to another language.

Thanks Dk,

However, I see it's still slow as R.

I am going to use Python because I hope that it's faster than R.

My data is usually > 1000000 rows (2 columns).

CT

What do you mean, slow?

x <- matrix(c(rnorm(1e6), rnorm(1e6)),ncol=2)
system.time(sum(x[x[,1] < 0, 2]))
  user  system elapsed 
 0.045   0.007   0.053

Thanks Steve,

Could you please help me improve this?

hh is a vector, x is a matrix:

length(hh) = n

dim(x) = c(N, 2)

Both n and N are very large.

I would like to sum:

for (i in 1:n)
    sum(x[(x[, 1] < h[i]) & x[, 2] > h[i + 1], 2])

please open a new discussion for each question you have, otherwise people will downvote you.

Rather don't open such questions at all here. As I said it's better posted on stackoverflow, but I predict you will be downvoted into the ground there for such a question.

Try the data.table package in R. Following on from Steve Lianoglou's example (see his comment):

x <- matrix(c(rnorm(1e6), rnorm(1e6)),ncol=2)
system.time(sum(x[x[,1] < 0, 2]))

   user  system elapsed 
  0.130   0.020   0.151 

library(data.table)
xDT <- data.table(x)
system.time(sum(xDT$V2[xDT$V1 < 0]))

   user  system elapsed 
  0.110   0.000   0.114 

# set the first column as a key
setkey(xDT, V1)

system.time(sum(xDT$V2[xDT$V1 < 0]))
   user  system elapsed 
  0.090   0.000   0.093

Log in to answer this question.