“ValueError: The truth value of a Series is ambiguous” 尽管使用了 ‘&’ 操作符。

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

Hitting "ValueError: The truth value of a Series is ambiguous" despite using '&'

问题

I am trying to replace values in a column based on two filter conditions. Despite using &, I am getting the following error:
> "ValueError: The truth value of a Series is ambiguous".

This is what I have tried:

  1. df = db.pd.DataFrame({"Fruits":["Apple","Apple","Apple","Orange","Orange","Orange"],\
  2. "Year": [2010,2011,2012,2010,2011,2012], \
  3. "Price": [2,3,1,1,2,3]})
  4. df["Change"] = ""
  5. df["Change"] = df.apply(lambda x: "Increase" if (x['Price'] < df.loc[(df["Year"]+1 == x['Year'])\
  6. & (df['Fruits'] == x['Fruits'])]['Price']) else '', axis = 1)
英文:

I am trying to replace values in a column based on two filter conditions. Despite using &, I am getting the following error:
> "ValueError: The truth value of a Series is ambiguous".

This is what I have tried:

  1. df = db.pd.DataFrame({"Fruits":["Apple","Apple","Apple","Orange","Orange","Orange"],\
  2. "Year": [2010,2011,2012,2010,2011,2012], \
  3. "Price": [2,3,1,1,2,3]})
  4. df["Change"] = ""
  5. df["Change"] = df.apply(lambda x: "Increase" if (x['Price'] < df.loc[(df["Year"]+1 == x['Year'])\
  6. & (df['Fruits'] == x['Fruits'])]['Price']) else '', axis = 1)

答案1

得分: 0

你需要使用 .any(),它会在可迭代对象中的任何元素为真时返回 True,否则返回 False。所以,在你的情况下,它将识别所有满足条件的实例,并将值赋给列 ChangeIncrease

请改用以下方式:

  1. import pandas as pd
  2. df = pd.DataFrame({"Fruits":["Apple","Apple","Apple","Orange","Orange","Orange"],
  3. "Year": [2010,2011,2012,2010,2011,2012],
  4. "Price": [2,3,1,1,2,3]})
  5. df["Change"] = df.apply(lambda x: "Increase" if ((df["Year"] == x["Year"]-1) & (df["Fruits"] == x["Fruits"]) & (df["Price"] < x["Price"])).any() else "", axis=1)
  6. print(df)

这将产生以下结果:

  1. Fruits Year Price Change
  2. 0 Apple 2010 2
  3. 1 Apple 2011 3 Increase
  4. 2 Apple 2012 1
  5. 3 Orange 2010 1
  6. 4 Orange 2011 2 Increase
  7. 5 Orange 2012 3 Increase
英文:

You need to use .any() which returns True if any item in an iterable are true, otherwise it returns False. So, in your case it will identify all instances for which your conditions are True and assign Increase to the value of the column Change.

Do this instead:

  1. import pandas as pd
  2. df = pd.DataFrame({&quot;Fruits&quot;:[&quot;Apple&quot;,&quot;Apple&quot;,&quot;Apple&quot;,&quot;Orange&quot;,&quot;Orange&quot;,&quot;Orange&quot;],\
  3. &quot;Year&quot;: [2010,2011,2012,2010,2011,2012], \
  4. &quot;Price&quot;: [2,3,1,1,2,3]})
  5. df[&quot;Change&quot;] = df.apply(lambda x: &quot;Increase&quot; if ((df[&quot;Year&quot;] == x[&quot;Year&quot;]-1) &amp; (df[&quot;Fruits&quot;] == x[&quot;Fruits&quot;]) &amp; (df[&quot;Price&quot;] &lt; x[&quot;Price&quot;])).any() else &quot;&quot;, axis=1)
  6. print(df)

which gives

  1. Fruits Year Price Change
  2. 0 Apple 2010 2
  3. 1 Apple 2011 3 Increase
  4. 2 Apple 2012 1
  5. 3 Orange 2010 1
  6. 4 Orange 2011 2 Increase
  7. 5 Orange 2012 3 Increase

huangapple
  • 本文由 发表于 2023年5月7日 22:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194586.html
匿名

发表评论

匿名网友

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

确定