简体   繁体   中英

Printing specific row from a table

I have a .csv table named data that looks like the following:

Age name ID
15 Stan 9
4 Danny 2
7 Elle 6

However, I want to print out a list of names from the table but only if their age is under 10. So in this case [Danny, Elle] . Here is what I tried so far

if data['Age'] < 10:
     print(data['name'])

I get an error The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). which I don't understand. It would be helpful to know how to solve this and also understand the error.

If you print type(data['Age'] < 10) it will return pandas.core.series.Series , this is not a boolean type as you are expecting, it is an object which looks as follows:

In [7]: data['Age'] < 10
Out[7]:
0    False
1     True
2     True
Name: Age, dtype: bool

You can think of it as a set of boolean elements which are true if the index satisfies the that age < 10 and False otehrwise. You can use these values to retrieve a subset of your table and then print the name column as follows (it will select a subset where the values are True):

sub = data[data['Age'] < 10]
print(sub['name'])

What the error is telling you is that pandas cannot figure out a boolean value for a complex object such as Series. Put it simply, it is a bunch of indexes, for which one of those are you evaluating it to be true? This is why there are suggestions such as a.any() or a.all() , which can be used to convert all the boolean values for each index into a single boolean value (if any of them is true, or if all of them are true, respectively). For example:

In [23]: a = data['Age'] < 10

In [24]: print(a)
0    False
1     True
2     True
Name: Age, dtype: bool

In [25]: a.any()
Out[25]: True

In [26]: a.all()
Out[26]: False

a.any() evaluates to true because there is at least one index which is True (2 and 1). a.all() evaluates to False because not every index is False (0 is False).

To get all names where age < 10 you can use boolean-indexing:

mask = df["Age"] < 10
print(df.loc[mask, "name"])

Prints:

1    Danny
2     Elle
Name: name, dtype: object

data['Age'] < 10 evaluates to:

0    False
1     True
2     True
Name: Age, dtype: bool

so when you do if data['Age'] < 10: what should be the result? The error message The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). is telling you that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM