0

I have got the value of mean that is .0004 from the program given below.

dff = pd.read_csv("I01.csv")
avg = dff['A'].rolling(3).mean()
dff['I_mean'] = avg
window = []
listpos = 0 
for datapoint in dff.A:
    mean = dff.I_mean[listpos] 
    print(mean)
    if (datapoint < mean) and (len(window) < 1):
        listpos += 1

After simulation the program if statement is not working. It shows error.

  A    0.0004
  dtype: float64

ValueError                                Traceback (most recent call last)
<ipython-input-12-49a6c73fdd23> in <module>()
     74     mean = dff.I_mean[listpos] 
     75     print(mean)
---> 76     if (datapoint < mean) & (len(window) < 1):
     77         listpos += 1

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\generic.py in __nonzero__(self)
   1328     def __nonzero__(self):
   1329         raise ValueError(
-> 1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1332         )


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

.0004 is the mean value. If I put this mean value (.0004) directly on the code then my program shows no error. Why it's happening? What's wrong in the code?

Abc
  • 19
  • 6
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Greg Schmit Nov 20 '20 at 15:53
  • Also, posting a full traceback is a good idea. – Greg Schmit Nov 20 '20 at 15:55
  • This also might help: https://stackoverflow.com/questions/53830081/python-pandas-the-truth-value-of-a-series-is-ambiguous – Greg Schmit Nov 20 '20 at 16:14
  • What happens if after `print(mean)` you put `print(type(datapoint))`? Are you comparing a series to a single value? – Greg Schmit Nov 20 '20 at 16:25
  • I don't have your CSV so I cannot replicate your issue. You probably should provide a [mcve]. – Greg Schmit Nov 20 '20 at 16:26
  • This is minimal reproducible example. The actual program is too long. – Abc Nov 20 '20 at 16:31
  • How is it reproducible if I don't have the CSV? – Greg Schmit Nov 20 '20 at 16:33
  • @GregSchmit the output of print(type (datapoint)) is dtype: float64 – Abc Nov 20 '20 at 16:35
  • @GregSchmit CSV data has 3000 entries. Should I update a few data here? – Abc Nov 20 '20 at 16:40
  • Right now this example isn't reproducible, nor is it minimal. Rather than complicate it with a CSV, produce an example which uses some static data, like `dff = pd.Series([1,2,3])` (or whatever) and try to replicate your error. I suspect that in that process you'll figure out what is wrong. Perhaps the old wording will help: I need a **minimal**, **complete**, and **verifiable** example in order to help you. – Greg Schmit Nov 20 '20 at 17:18
  • Put another way: right now when I paste your code into a Python file, it doesn't run. Make it minimal and complete so I can run it on my system. – Greg Schmit Nov 20 '20 at 17:19

0 Answers0