0

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?

2 Answers 2

1

One alternative is to use next(..., None), which returns None if the iterator is empty:

b = next(iter(df[(df.X == 7) & (df.Y == 18)].Z), None)
print(b)
# None
Sign up to request clarification or add additional context in comments.

Comments

0

2 things - your result is empty, hence error - .item() apparently throws an error on empty pd.Series. Secondly - the more canonical way of achieving what you're after would be:

>>> b = df.loc[(df.X == 7) & (df.Y == 18), "Z"].values
>>> if len(b) == 0: b=None
...

Also prevents error .item() generates.

4 Comments

This does not assign the variable b to None but to an empty array.
which is correct - since 0 rows match your filtering criteria.
I do want it to be assigned to None if 0 rows match your filtering criteria
just add if statement - see my edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.