Search code examples
pythonpandasnumpyboolean-logiccase-sensitive

Case insensitive logic in Python numpy arrays (and Pandas Index)


I have a Pandas Series, s, and spliced it::

print(s)
A            {B, A}
B     {B,  A   , E}
C          {B,  C}
D            {D, A}
E        {B, E,  C}
dtype: object

f = s.index
p = s.values

f is now a Pandas Index; p is a numpy array. I then strip the whitespaces.

I now want to 'cross-check', see which letters are in each row and column::

cross_check = (p[:, None] & [{x} for x in f]).astype(bool)
print(cross_check)

array([[ True,  True, False, False, False],
       [ True,  True, False, False,  True],
       [False,  True,  True, False, False],
       [ True, False, False,  True, False],
       [False,  True,  True, False,  True]], dtype=bool)

This is great, but fails if the case doesn't match i.e. "B" is 'b' in the first row.

How do I perform the logic and be case-insensitive?? Thanks!!


Solution

  • You can use list comprehension for convert sets to upper with strip:

    s = pd.Series([set(['B','A']), 
                   set(['B', ' a   ', 'E']),
                   set(['B','  C']),    
                   set(['d','A']),
                   set(['B','E', '  c'])], index=list('aBCDE'))
    print (s)
    a           {B, A}
    B    {B, E,  a   }
    C         {  C, B}
    D           {d, A}
    E      {  c, B, E}
    
    f = s.index.str.upper().str.strip()
    p = np.array([set([x.upper().strip() for x in item]) for item in s.values])
    print (p)
    [{'B', 'A'} {'B', 'E', 'A'} {'B', 'C'} {'D', 'A'} {'B', 'E', 'C'}]
    
    cross_check = (p[:, None] & [{x} for x in f]).astype(bool)
    print (cross_check)
    
    [[ True  True False False False]
     [ True  True False False  True]
     [False  True  True False False]
     [ True False False  True False]
     [False  True  True False  True]]
    

    For me Zero solution working nice too:

    p = s.apply(lambda x: {v.strip().upper() for v in x})
    print (p)
    A       {B, A}
    B    {B, E, A}
    C       {B, C}
    D       {D, A}
    E    {B, E, C}
    dtype: object