pandas 从现有数据帧 python 中选择特定行创建一个新的数据帧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40885318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:32:55  来源:igfitidea点击:

create a new dataframe from selecting specific rows from existing dataframe python

pythonpandas

提问by Shubham R

i have a table in my pandas dataframe. df

我的Pandas数据框中有一张表。df

id count price
1    2     100
2    7      25
3    3     720
4    7     221
5    8     212
6    2     200

i want to create a new dataframe(df2) from this, selecting rows where count is 2 and price is 100,and count is 7 and price is 221

我想从中创建一个新的数据框(df2),选择计数为 2,价格为 100,计数为 7,价格为 221 的行

my output should be df2 =

我的输出应该是 df2 =

id count price
1    2     100
4    7     221

i am trying using df[df['count'] == '2' & df['price'] == '100']

我正在尝试使用 df[df['count'] == '2' & df['price'] == '100']

but getting error

但得到错误

TypeError: cannot compare a dtyped [object] array with a scalar of type [bool]

回答by jezrael

You nedd add ()because &has higher precedence than ==:

你 nedd add()因为&具有更高的优先级==

df3 = df[(df['count'] == '2') & (df['price'] == '100')]
print (df3)
  id count price
0  1     2   100

If need check multiple values use isin:

如果需要检查多个值,请使用isin

df4 = df[(df['count'].isin(['2','7'])) & (df['price'].isin(['100', '221']))]
print (df4)
  id count price
0  1     2   100
3  4     7   221

But if check numeric, use:

但如果检查数字,请使用:

df3 = df[(df['count'] == 2) & (df['price'] == 100)]
print (df3)

df4 = df[(df['count'].isin([2,7])) & (df['price'].isin([100, 221]))]
print (df4)