lundi 22 mars 2021

I do not understand what is meant with: Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

I have a course for uni where we started coding for interpreting data into dataframes, graphs, etc... For one of our assignments, data from an experiment using mouse movement has been given to us. We had different options we could use the data for, and I chose trying to find the amount of times the mouse changes its path. I thought that the best way for doing this is finding the difference in the pixels and assigning (using a loop) the certain differences to their directions (this might not be the best approach, but I am trying :)). After assigning directions I could again look at the differences. If these differences != 0, the path would change. I sadly did not get past the first part as I got an error. I used this code:

if (df_paths_clean['x'].diff() == 0) & (df_paths_clean['y'].diff() > 0):
        df_paths_clean['direction'] == '1'
        
elif (df_paths_clean['x'].diff() > 0) & (df_paths_clean['y'].diff()) > 0:
        df_paths_clean['direction'] == '2'
        
elif (df_paths_clean['x'].diff() > 0) & (df_paths_clean['y'].diff() == 0):
        df_paths_clean['direction'] == '3'
        
elif (df_paths_clean['x'].diff() > 0) & (df_paths_clean['y'].diff() < 0):
        df_paths_clean['direction'] == '4'
        
elif (df_paths_clean['x'].diff() == 0) & (df_paths_clean['y'].diff() < 0):
        df_paths_clean['direction'] == '5'
        
elif (df_paths_clean['x'].diff() < 0) & (df_paths_clean['y'].diff() < 0):
        df_paths_clean['direction'] == '6'
        
elif (df_paths_clean['x'].diff() < 0) & (df_paths_clean['y'].diff() == 0):
        df_paths_clean['direction'] == '7'
        
elif (df_paths_clean['x'].diff() < 0) & (df_paths_clean['y'].diff() > 0):
        df_paths_clean['direction'] == '8'

else: (df_paths_clean['direction'] == '0')

and got this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-87-7e2d60410e6c> in <module>
      6 
      7 
----> 8 if (df_paths_clean['x'].diff() == 0) & (df_paths_clean['y'].diff() > 0):
      9         df_paths_clean['direction'] == 'N'
     10 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1327 
   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()."

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

Being new to this all, I do not really know what to do and fellow students don't know how to do it either! Thanks in advance for all the help :) !

Aucun commentaire:

Enregistrer un commentaire