Jaynes01
Jaynes01

Reputation: 531

Pandas Boolean selection updated?

I was used to getting a single number, which told me how many cases are TRUE for either for the conditions in the code. However, since I used conda update all I now get a list of values with either 0 or 1. I wonder what is now the simplest method in pandas to get this task done. I guess that this is a pandas update. I did a google search but could not find that they changed boolean indexing. What is the easiest way to get this sum of booleans (I know how to get it but I cannot imagine that this extra step is required).

import pandas as pd
import numpy as np

x = np.random.randint(10,size=10)
y = np.random.randint(10,size=10)
d ={}
d['x'] = x
d['y'] = y
df = pd.DataFrame(d)
sum([df['x']>=6] or [df['y']<=3])

Upvotes: 1

Views: 60

Answers (1)

akuiper
akuiper

Reputation: 214927

You need to use vectorized or |:

(df.x.ge(6) | df.y.le(3)).sum()
# 9

Or: ((df.y <= 3) | (df.x >= 6)).sum(), sum((df.y <= 3) | (df.x >= 6)).

Upvotes: 3

Related Questions