I'm trying to get values in column z that contains null values or integers:
df = pd.DataFrame({'X': [1, 2, 3, 4],
'Y': [2, 10, 13, 18],
'Z': [3, None, 5, None]})
a = df[(df.X == 1) & (df.Y == 2)].Z.item()
print(a)
#output: 3
b = df[(df.X == 7) & (df.Y == 18)].Z.item()
print(b)
#output: error
It throws a value error: can only convert an array of size 1 to a Python scalar. Because the data frame resulting from filtering the X and Y columns is empty. I want to assign variable b to None if the data frame is empty.
I tried the following, and it works:
#checking the length of the dataframe
b = df[(df.X == 1) & (df.Y == 2)].Z.item() if (len(df[(df.X == 7) & (df.Y == 18)]) == 1) else None
print(b)
# output: None
Is there a better way to do it?