gpt4 book ai didi

python - 从数据框中提取数据

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:48 25 4
gpt4 key购买 nike

我有一个 python 数据框,我试图从中提取一些数据:

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

我想提取与Car类型相关的数据。所以我所做的是:

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

但我收到此错误:

ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

谁能告诉我我做错了什么?谢谢。

最佳答案

试试这个:df[(df.Type=="Car") | (df.Type=="Van")]

例如,

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

print(df)

产生

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

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

产生

    Type    Value
0 Car 10
1 Van 15
2 Car 14

关于python - 从数据框中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57113298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com