mercredi 29 mars 2017

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

I have sorted my dataframe so that the time is in chronological order. The column 'Group' gives rows that belong to the same group, the same name (in this case, the name is the time of the first row that belongs to the group).

A 'group' will consist of at least 2 rows. Not all rows will belong to a group.

I am trying to create a new column named 'Bucket' that will give the rows that belong to a group the value 'yes' and those rows which do not belong to a group the value '0'. I am getting an error.

The code is as follows:

data['Bucket'] = 0

if (data['Group'] == data['Group'].shift(-1)) & (data['Group'] == data['Group'].shift(1)):

    data['Bucket'] = 'Yes'

elif (data['Group'] == data['Group'].shift(-1)) & (data['Group'] != data['Group'].shift(1)):

    data['Bucket'] = 'Yes'
elif (data['Group'] != data['Group'].shift(-1)) & (data['Group'] == data['Group'].shift(1)):

    data['Bucket'] = 'Yes'
else:

    data['Bucket'] = '0'


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

Aucun commentaire:

Enregistrer un commentaire