如何添加带有2个参数的查询。

huangapple go评论90阅读模式
英文:

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(&#39;Quantity &lt; 0&#39; )

i need to add one more parameter

online_rt.query(&#39;Quantity &lt; 0&#39; and &#39;country=Hong Kong&#39;) 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[&#39;Quantity&#39;]&lt;0) &amp; (online_rt[&#39;country&#39;]==&#39;Hong Kong&#39;)]

答案2

得分: 0

使用 DataFrame.query&#39;Hong Kong&#39;

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 运行方式如 &amp;

filtered_df = online_rt.query("Quantity < 0 and country == 'Hong Kong'")
print(filtered_df)
   Quantity    country
0        -1  Hong Kong
英文:

Use DataFrame.query with &#39;Hong Kong&#39; :

online_rt = pd.DataFrame({&#39;Quantity&#39;:[-1,2,0],
                          &#39;country&#39;:[&#39;Hong Kong&#39;,&#39;USA&#39;,&#39;Hong Kong&#39;]})

filtered_df = online_rt.query(&quot;Quantity &lt; 0 &amp; country == &#39;Hong Kong&#39;&quot;)
print (filtered_df)
   Quantity    country
0        -1  Hong Kong

Another solution with and working in query like &amp;:

filtered_df = online_rt.query(&quot;Quantity &lt; 0 and country == &#39;Hong Kong&#39;&quot;)
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(&#39;Quantity &lt; 0 &amp; country == Hong Kong&#39;)

huangapple
  • 本文由 发表于 2020年1月3日 18:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定