Reputation: 61
I have to read excel and if column value meet some condition i have to add some thing. Below are the input excel
I just need to take Name and Values if Value > 50
Below is my script.
df = pd.read_excel(file)
print (df[df['Value'] > 50]) #prints the name and values
if df[df['Value'] > 50]:
print (df['Value'])
I am getting ValueError. ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). Please help me.
Upvotes: 0
Views: 284
Reputation: 862396
If want test at least one value >50
before filtering by mask use any
for scalar True
or False
:
df = pd.read_excel(file)
mask = df['Value'] > 50
if mask.any():
df1 = df[mask]
print (df1)
If if
statement is not necessary:
df1 = df[df['Value'] > 50]
print (df1)
You can also check using if truth statements with pandas.
Upvotes: 1
Reputation: 3495
Assuming you want to print all the values greater than 50, do:
print(df[df['Value'] > 50].values)
Upvotes: 0