1
import pandas as pd
from pandas import DataFrame
import math as ma

df = pd.read_csv("/Users/aaronhuang/Desktop/ffp/exfileCLEAN.csv")
df_top = df.head()
num = pd.read_csv('/Users/aaronhuang/Desktop/ffp/exfileCLEAN.csv', usecols=[1])


stand = round(num.std(), 4)
conf = stand * 3
avgs = round(num.mean(), 4)
avg = 19.31322603
ran = 0.425812854

i = 0
while(i < len(df['Magnitude '])):
    if(abs(df['Magnitude '][i] - avgs) > conf):
        print(df['Magnitude '][i])

    i += 1

When I try and run this, it gives me:

Traceback (most recent call last):
  File "/Users/aaronhuang/PycharmProjects/EXTTEst/Code sandbox.py", line 18, in <module>
    if(abs(df['Magnitude '][i] - avgs) > conf):
  File "/Users/aaronhuang/.conda/envs/EXTTEst/lib/python3.8/site- 
packages/pandas/core/generic.py", line 1478, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), 
a.any() or a.all().

Process finished with exit code 1

How can I fix this so I do not get this error anymore? Also When it prints the Values at the end, is there a way can I have it print out the cell in the first column in the CSV instead.

Any help would be greatly appreciated.

Thanks

  • It looks like you are probably expecting to get single values for `stand = round(num.std(), 4)` and `avgs = round(num.mean(), 4)`. Problem is the mean and std methods are only calculated across one dimension ("axis") of your table, so those values are probably Series types. To get the numerical result you would need to do `num.mean().mean()`. Aside from that, the values you are interested in getting with that loop would be easier with something like `values = [x for x in df['Magnitude'] if (abs(x) - avgs > conf)]` and then if you wanted to print them just `for x in values: print(x)` – teepee Jun 04 '20 at 00:07

0 Answers0