Prathamesh Deshmukh
Prathamesh Deshmukh

Reputation: 1

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

I have defined a function:

def convert(x):
        if [(x != float(0)) & (x!= float(1))]:
                if x > 0.5:
                    return float(1)
                else:
                    return float(0)
        else:
            return x

Getting the output as expected:

convert(0.7) ---> 1.0
convert(0.5) ---> 0.0

But when I am applying this function to the dataframe as shown below, I am getting the ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

df_imputed['encoded_gender'].apply(convert)

Below is the dataframe df_imputed:


   encoded_gender         age     bmi
0     1.0                 19.0  27.900
1     0.0                 18.0  33.770
2     0.0                 28.0  33.000
3     0.0                 33.0  22.705
4     0.0                 32.0  28.880
...   ...   ... ...
1333  0.0                 50.0  30.970
1334  1.0                 18.0  31.920
1335  1.0                 18.0  36.850
1336  1.0                 21.0  25.800
1337  1.0                 61.0  29.070
1338 rows × 3 columns
df_imputed['encoded_gender'].isnull().sum()

o/p:
encoded_gender    0
dtype: int64
df_imputed['encoded_gender'].value_counts()

o/p:
(encoded_gender,)
0.0                  648
1.0                  640
0.6                   18
0.5                   11
0.4                    7
0.7                    6
0.3                    5
0.2                    3
dtype: int64

I've read multiple posts that mentioned the same error, but none of them solved this particular type of issue. Could you please help me find out what changes I need to do in code

Upvotes: 0

Views: 58

Answers (0)

Related Questions