0

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.

eshirvana
  • 23,227
  • 3
  • 22
  • 38
cff
  • 23
  • 1
  • 8
  • you have multiple rows, so you want to check for each row? please provide sample data and a minimal reproducible code /df and show us your desired output – eshirvana Mar 21 '22 at 20:12
  • The conditions `df_total[...]` will select zero or more rows from the DataFrame - okay, now what? Do you want to check if there are any rows selected? Do you want to check if all the rows have some value? Do you want to check if *any* of the rows have some value? Something else? What should `if` mean if it is just given some rows? The error message means exactly what it says: Pandas can't decide what you mean, so you have to tell it. Please see the linked duplicate. – Karl Knechtel Mar 21 '22 at 20:35
  • @eshirvana Yes, I want to check each row in order to now if follows the conditions... – cff Mar 22 '22 at 16:30
  • @KarlKnechtel I want to verify several conditions for each one of the columns in the dataframe... maybe it's more clear by this way? Thanks! – cff Mar 22 '22 at 16:32

1 Answers1

0

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")
keramat
  • 4,328
  • 6
  • 25
  • 38