Elementary logical AND indefinite Pandas Series

Let's say I have a list / iterable of n (where n is unknown to the function) Pandas Series that represent boolean logical indexes and I would like them all to be different and use the resulting series to index the DataFrame.

I am currently using np.logical_and(x1,x2)

a for loop for this. I had no luck with itertools.izip

or zip

. The pandas.Series object didn't seem to like them.

I've been scratching my head for a while about the fact that I may not see why this seems to result in a series of gates, but then I get IndexingError: Unalignable boolean Series key provided

after execution.

Any thoughts? I feel, since this is ndarray

, there must be some apparently clean way of doing this.

+1


source to share


1 answer


Assuming I understand you, you can use logical_and.reduce

. Starting with the series listing:

>>> ss = [pd.Series([ True, False,  True, False,  True]), pd.Series([False,  True,  True, False, False]), pd.Series([False, False,  True, False,  True]), pd.Series([False,  True,  True, False, False]), pd.Series([ True,  True,  True,  True, False])]

      

which will look like

>>> pd.DataFrame(ss)
       0      1     2      3      4
0   True  False  True  False   True
1  False   True  True  False  False
2  False  False  True  False   True
3  False   True  True  False  False
4   True   True  True   True  False

[5 rows x 5 columns]

      

if it was a dataframe, you can minify it column by column:



>>> np.logical_and.reduce(ss)
array([False, False,  True, False, False], dtype=bool)

      

or go axis=1

if you want a different direction.

Remember that you can also use any

and all

, for example.

>>> df = pd.DataFrame(ss)
>>> df.all()
0    False
1    False
2     True
3    False
4    False
dtype: bool

      

+2


source







All Articles