【发布时间】:2017-04-14 14:18:50
【问题描述】:
我的 pandas 数据框中有一个表格。 df
id count price
1 2 100
2 7 25
3 3 720
4 7 221
5 8 212
6 2 200
我想从中创建一个新的数据框(df2),选择计数为 2 且价格为 100 且计数为 7 且价格为 221 的行
我的输出应该是 df2 =
id count price
1 2 100
4 7 221
我正在尝试使用df[df['count'] == '2' & df['price'] == '100']
但出现错误
TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]
【问题讨论】:
-
经典之作:
df[(df['count'] == '2') & (df['price'] == '100')].