3

which is the fastest way to achieve the following:

I'm using a Pandas Dataframe (NxN) and i want to iterate over each row and each element to check if the element is greater than the rows mean. If it is greater i want to change the element value to 1.

I calculate the mean value using :

mean_value = df.ix[elementid].mean(axis=0)

but iterating over each element and checking if it is >= mean_value with a nested loop is really slow.

4
  • You are accessing every element, what makes you think you can do better than O(nm). Commented Apr 7, 2016 at 17:59
  • I'm just hoping there is function in pandas to apply the value 1 row-wise if the elements are greater than the mean Commented Apr 7, 2016 at 18:02
  • That function would do exactly the same thing as doing it by hand. You are changing every element of the array, therefore you have to access every element of the array. You can't do it faster Commented Apr 7, 2016 at 18:03
  • 1
    I'm doing the loops in python and i thought pandas is partly written in cython or based on libraries which are written in cython and therefore would be faster Commented Apr 7, 2016 at 18:04

1 Answer 1

6

You can first count mean by rows, then comparing with ge and where mask add 1:

print df
   a  b  c
0  0  1  2
1  0  1  2
2  1  1  2
3  1  0  1
4  1  1  2
5  0  0  1

mean_value = df.mean(axis=1)
print mean_value
0    1.000000
1    1.000000
2    1.333333
3    0.666667
4    1.333333
5    0.333333

mask = df.ge(mean_value, axis=0)
print mask
       a      b     c
0  False   True  True
1  False   True  True
2  False  False  True
3   True  False  True
4  False  False  True
5  False  False  True
print df.mask(mask, 1)
   a  b  c
0  0  1  1
1  0  1  1
2  1  1  1
3  1  0  1
4  1  1  1
5  0  0  1
Sign up to request clarification or add additional context in comments.

4 Comments

That was neat use of mask and ge!
very elegant solution +1
Looks good except for final result. Don't you just want df.mask(df.gt(df.mean(axis=1)), 1)?
Glad can help you! Good luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.