pandas 数据框的 if 条件

发布于 2025-01-16 00:52:04 字数 989 浏览 0 评论 0原文

我对此代码有疑问,我不知道我是否选择了正确的数据帧:

if (df_total[(df_total["name"] == "STREET 292") & (df_total["free_motobikes"] != 0) & (df_total["HOUR"] == 7) & (df_total["MINUTES"]<= 50)]):
  print("User can take a motobike at STREET 292 by aprox. 7:50 am ")
elif (df_total[(df_total["name"] == "STREET 6") & (df_total["empty_slots"] != 0) & (df_total["HOUR"] <= 8)]):
  print("User can park the motobike at AV. DE LA CATEDRAL, 6 by aprox 8:00 am")
else:
  print("User cannot take a motobike neither park it")

输出:

ValueError               

def __nonzero__(self):
1537         raise ValueError(
\-\> 1538             f"The truth value of a {type(self).__name__} is ambiguous. "
1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1540         )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我希望程序检查这两个条件,以便知道用户是否可以在这段时间内骑摩托车。

I have a doubt regarding this code and I don't know if I'm selecting correct the dataframe:

if (df_total[(df_total["name"] == "STREET 292") & (df_total["free_motobikes"] != 0) & (df_total["HOUR"] == 7) & (df_total["MINUTES"]<= 50)]):
  print("User can take a motobike at STREET 292 by aprox. 7:50 am ")
elif (df_total[(df_total["name"] == "STREET 6") & (df_total["empty_slots"] != 0) & (df_total["HOUR"] <= 8)]):
  print("User can park the motobike at AV. DE LA CATEDRAL, 6 by aprox 8:00 am")
else:
  print("User cannot take a motobike neither park it")

Output:

ValueError               

def __nonzero__(self):
1537         raise ValueError(
\-\> 1538             f"The truth value of a {type(self).__name__} is ambiguous. "
1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1540         )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I want that the program check both conditions in order to know if the user could take a motorbike in this frame of time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2025-01-23 00:52:04

使用:

if len(df_total[(df_total["name"] == "STREET 292") & (df_total["free_motobikes"] != 0) & (df_total["HOUR"] == 7) & (df_total["MINUTES"]<= 50)])>0:
  print("User can take a motobike at STREET 292 by aprox. 7:50 am ")
elif len(df_total[(df_total["name"] == "STREET 6") & (df_total["empty_slots"] != 0) & (df_total["HOUR"] <= 8)]):
  print("User can park the motobike at AV. DE LA CATEDRAL, 6 by aprox 8:00 am")
else:
  print("User cannot take a motobike neither park it")

Use:

if len(df_total[(df_total["name"] == "STREET 292") & (df_total["free_motobikes"] != 0) & (df_total["HOUR"] == 7) & (df_total["MINUTES"]<= 50)])>0:
  print("User can take a motobike at STREET 292 by aprox. 7:50 am ")
elif len(df_total[(df_total["name"] == "STREET 6") & (df_total["empty_slots"] != 0) & (df_total["HOUR"] <= 8)]):
  print("User can park the motobike at AV. DE LA CATEDRAL, 6 by aprox 8:00 am")
else:
  print("User cannot take a motobike neither park it")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文