Problem with If- Else Conditiones, How can I resolve it?

Question:

The problem is that I want that the code shows the graph if the Value of "Recordinaciones" is > 1, and shows "No hay Recorinaciones Dobles" if <1 but I have some strange issue. Hope someone can help me!
The problem is:

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

Here it’s the code:

import pandas as pd

doc = input('Ingresa el nombre del archivo: ')
print(f'Ingresaste {doc}')

df=pd.read_excel(doc+'.xlsx')
df['Recordinaciones'] = df.apply(lambda _: '', axis=1)
df['Cantidad'] = df.apply(lambda _: '', axis=1)

rcs=df[['Cliente','# Externo','Recordinaciones']].groupby(['Cliente','# Externo']).count().reset_index().sort_values(['Recordinaciones'],ascending=False)

Recoordinaciones = rcs['Recordinaciones']  
if Recoordinaciones > 1:                        # Pregunto si x es mayor a 1
    print(rcs[(rcs['Recordinaciones'] > 1)])    
else:
  print( "No hay Recorinaciones Dobles")            # cumple, ejecuto esto

Error Message

Ingresa el nombre del archivo: Test Feb
Ingresaste Test Feb

ValueError Traceback (most recent call last)
in
11
12 Recoordinaciones = rcs[‘Recordinaciones’]
—> 13 if Recoordinaciones > 1: # Pregunto si x es mayor a 1
14 print(rcs[(rcs[‘Recordinaciones’] > 1)])
15 else:

/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in nonzero(self)
1536 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 Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

enter image description here

Asked By: Franco Vera

||

Answers:

try this:

Recoordinaciones = rcs.loc[rcs['Recordinaciones'] > 1]['Recordinaciones'].tolist()

if len(Recoordinaciones) == 0:
    print('no values >1')
else:
    for r in Recoordinaciones: 
        print(r)

basically the loc function receives a condition with a boolean outcome and locates the rows where this condition is met.

Answered By: maxxel_
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.