英文:
How to add query with 2 parameters
问题
我的数据框如下 online_rt
InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country
141 C536379 D Discount -1 12/1/10 9:41 27.50 14527.0 United Kingdom
154 C536383 35004C SET OF 3 COLOURED FLYING DUCKS -1 12/1/10 9:49 4.65 15311.0 United Kingdom
235 C536391 22556 PLASTERS IN TIN CIRCUS PARADE -12 12/1/10 10:24 1.65 17548.0 United Kingdom
236 C536391 21984 PACK OF 12 PINK PAISLEY TISSUES -24 12/1/10 10:24 0.29 17548.0 United Kingdom
237 C536391 21983 PACK OF 12 BLUE PAISLEY TISSUES -24 12/1/10 10:24 0.29 17548.0 Hong Kong
需要找到 Quantity
为负数且 Country
为 "Hong Kong" 的参数。
online_rt.query('Quantity < 0 and Country == "Hong Kong"')
抛出错误。
英文:
My data frame is below online_rt
InvoiceNo StockCode Description Quantity InvoiceDate UnitPrice CustomerID Country
141 C536379 D Discount -1 12/1/10 9:41 27.50 14527.0 United Kingdom
154 C536383 35004C SET OF 3 COLOURED FLYING DUCKS -1 12/1/10 9:49 4.65 15311.0 United Kingdom
235 C536391 22556 PLASTERS IN TIN CIRCUS PARADE -12 12/1/10 10:24 1.65 17548.0 United Kingdom
236 C536391 21984 PACK OF 12 PINK PAISLEY TISSUES -24 12/1/10 10:24 0.29 17548.0 United Kingdom
237 C536391 21983 PACK OF 12 BLUE PAISLEY TISSUES -24 12/1/10 10:24 0.29 17548.0 Hong Kong
need to find the parameters where Quantity
is negative and country=Hong Kong
online_rt.query('Quantity < 0' )
i need to add one more parameter
online_rt.query('Quantity < 0' and 'country=Hong Kong')
thrown error
答案1
得分: 1
假设online_rt
是一个pandas DataFrame,您可以使用:
filtered_df = online_rt[(online_rt['Quantity'] < 0) & (online_rt['country'] == 'Hong Kong')]
英文:
Assuming that online_rt
is a pandas DataFrame, you can use:
filtered_df = online_rt[(online_rt['Quantity']<0) & (online_rt['country']=='Hong Kong')]
答案2
得分: 0
使用 DataFrame.query
与 'Hong Kong'
:
online_rt = pd.DataFrame({'Quantity': [-1, 2, 0],
'country': ['Hong Kong', 'USA', 'Hong Kong']})
filtered_df = online_rt.query("Quantity < 0 & country == 'Hong Kong'")
print(filtered_df)
Quantity country
0 -1 Hong Kong
另一种使用 query
中的 and
运行方式如 &
:
filtered_df = online_rt.query("Quantity < 0 and country == 'Hong Kong'")
print(filtered_df)
Quantity country
0 -1 Hong Kong
英文:
Use DataFrame.query
with 'Hong Kong'
:
online_rt = pd.DataFrame({'Quantity':[-1,2,0],
'country':['Hong Kong','USA','Hong Kong']})
filtered_df = online_rt.query("Quantity < 0 & country == 'Hong Kong'")
print (filtered_df)
Quantity country
0 -1 Hong Kong
Another solution with and
working in query
like &
:
filtered_df = online_rt.query("Quantity < 0 and country == 'Hong Kong'")
print (filtered_df)
Quantity country
0 -1 Hong Kong
答案3
得分: 0
只需像这样操作:
online_rt.query('Quantity < 0 & country == Hong Kong')
英文:
Just do it like this
online_rt.query('Quantity < 0 & country == Hong Kong')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论