0

I am trying to look up rows containing non-Null values of a specific item. Both Item_Weight (float) and Item_Identifier (str) come from different columns of the dataframe.

Here's the code to generate the DF:

import numpy as np
import pandas as pd

df = pd.DataFrame()
np.random.seed(seed=6)
df['col0']=np.random.randn(100)
lett=['a','b','c','d']
df['col1']=np.random.choice(lett,100)
toz = np.random.randint(0,100,15)
#Randomly set some values of col0 to null.
df.loc[toz,'col0']=np.NaN
df.loc[df.loc[df['col0'].isnull()==False] & df.loc[df['col1']=='b']]

Throws TypeError:

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

df.loc[(df['col0'].isnull() == False) & (df.loc[df['col1'] == 'b'])]

Throws ValueError:

ValueError: operands could not be broadcast together with shapes (56,) (2,)

Instead if I query like:

df.loc[(df['col0'].isnull() == False) & (df.loc[df['col1'] == 'b'])]

I get another TypeError:

TypeError: Could not operate False with block values unsupported operand type(s) for &: 'bool' and 'float'

I am able to query each part separately (col0 == Null or col1 == 'b'). But I cannot use the & operation on them. Is there a workaround? What am I doing wrong?

Sachin Myneni
  • 273
  • 3
  • 14
  • i think your answer is here https://stackoverflow.com/questions/22231438/bitwise-operations-in-pandas-that-return-numbers-rather-than-bools – durjoy Jan 04 '18 at 20:50
  • Ack! I just realized I made a mistake in my code above.. Instead of df.loc[toz,'col0']='' it should be: df.loc[toz,'col0']=np.NaN I will attempt to fix that in the question now. – Sachin Myneni Jan 04 '18 at 22:11
  • 1
    `df.loc[~df['col0'].isnull() & df['col1'].eq('b')]` – cs95 Jan 04 '18 at 22:12

1 Answers1

1

Change it to this:

df_train_1.loc[ ( ~df_train_1['Item_Weight'].isnull() ) & (df_train_1['Item_Identifier'] == 'FDP10') ]
Gabriel A
  • 1,779
  • 9
  • 12
  • This gave me an error. `ValueError: operands could not be broadcast together with shapes (59689,) (8527,) ` . I will try to create sample code and update my questions. – Sachin Myneni Jan 04 '18 at 21:34
  • There was an error in your code that I didn’t notice. I have updated my answer to fix it – Gabriel A Jan 04 '18 at 22:04