简体   繁体   中英

Extracting data from dataframe

I have a python dataframe from which im trying to extract some data:

frame   id        type         truncated
0       -1      DontCare        -1
0       10        Car            0  
0       13       Misc            0
0       11        Car            1
0       12        Car            1

and I want to extract data related to the Car type. So what I did is:

 for column in labels['type'].items():
        if column == 'DontCare':
            continue
        if column == "Car" or "Van":
            print('car')
        else:
            print('no car')

But I get this error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Can anyone tell me what I did wrong? Thank you.

Try this: df[(df.Type=="Car") | (df.Type=="Van")] df[(df.Type=="Car") | (df.Type=="Van")]

For example,

data = [['Car', 10], ['Van', 15], ['Car', 14], ['DNC', 11]] 
df = pd.DataFrame(data, columns = ['Type', 'Value']) 

print(df) 

produces

    Type    Value
0   Car     10
1   Van     15
2   Car     14
3   DNC     11

and

print(df[(df.Type=="Car") | (df.Type=="Van")])

produces

    Type    Value
0   Car     10
1   Van     15
2   Car     14

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