Comparison operator with Lambda Expression is not working as expected

Ananthu

I am trying to replace the null values in a column based on categorical value of another column.But the == operator is making me regret all the big decision in my life. I have 8523 rows and 12 columns in Train set, of which 7 are categorical and 5 are numerical.

Columns are 'Item_Identifier', 'Item_Weight', 'Item_Fat_Content', 'Item_Visibility', 'Item_Type', 'Item_MRP', 'Outlet_Identifier', 'Outlet_Establishment_Year', 'Outlet_Size', 'Outlet_Location_Type', 'Outlet_Type', 'Item_Outlet_Sales'

I want to fill the NaN values(float dtype) in the 'Item_Weight' column based on the categorical value of 'Outlet_Location_Type'. I have a dictionary(city_type_mean) with the categorical values as keys and the corresponding values to be replaced as values. I used the following code

train["Item_Weight"] = train.apply(lambda x: city_type_mean[x['Outlet_Location_Type']] if x["Item_Weight"] == np.nan else x["Item_Weight"], axis=1) 

But the Nan value remains unaffected. I am attaching a train data sample following the problemmatic code image. Train data sample.problemmaticcode snippet The problem I've so far troubleshooted was the above if condition always evaluates to false leading to else being executed. And I've tried the condition with is and pd.isnull() methods but to no avail.Any help with the problem is much appreciated.Also please intimate me before marking this question in case of duplication.

r.b.leon

can you please try isnan instead of == np.nan ?

train["Item_Weight"] = train.apply(lambda x: city_type_mean[x['Outlet_Location_Type']] if  np.isnan(x["Item_Weight"]) else x["Item_Weight"], axis=1) 

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Expression expected in ?: operator

分類Dev

Ternary Operator in jQuery not working as expected

分類Dev

Expression expected for logical OR operator to boolean in reactjs

分類Dev

LIKE operator in SQL Server not working as expected

分類Dev

unary operator prefix execution in recursion is not working as expected

分類Dev

VBA power operator (^) not working as expected in 64-bit VBA

分類Dev

How operator '=' is really working in the expression a = [x,y,z] = "123"?

分類Dev

Getting the error 'Expression expected.' when attempting a for loop on the else part of a ternary operator

分類Dev

Bash - Integer expression expected

分類Dev

numpy elementwise comparison with overloaded operator

分類Dev

Rspec and Rails NoMethodError for comparison operator

分類Dev

Operator associativity in prefix expression

分類Dev

Android: Databinding expression with enum comparison

分類Dev

Conditional statement, "unary operator expected"

分類Dev

AJAX call is not working as expected

分類Dev

jQuery .when().then() not working as expected

分類Dev

.vimrc file not working as expected

分類Dev

*ngIf not working as expected with observable

分類Dev

ItemIsAutoTristate flag not working as expected

分類Dev

KeyboardAvoidingView not working as expected on IOS

分類Dev

PowerShell variables not working as expected

分類Dev

XPath logical 'and' not working as expected?

分類Dev

CancellationTokenSource not working as expected on a TaskCompletionSource

分類Dev

string formatting not working as expected

分類Dev

Why is .on() Not working as expected in jquery?

分類Dev

Tuples in Scala not working as expected

分類Dev

strpos() not working as expected

分類Dev

PHP Dateformat not working as expected

分類Dev

Update not working as expected

Related 関連記事

  1. 1

    Expression expected in ?: operator

  2. 2

    Ternary Operator in jQuery not working as expected

  3. 3

    Expression expected for logical OR operator to boolean in reactjs

  4. 4

    LIKE operator in SQL Server not working as expected

  5. 5

    unary operator prefix execution in recursion is not working as expected

  6. 6

    VBA power operator (^) not working as expected in 64-bit VBA

  7. 7

    How operator '=' is really working in the expression a = [x,y,z] = "123"?

  8. 8

    Getting the error 'Expression expected.' when attempting a for loop on the else part of a ternary operator

  9. 9

    Bash - Integer expression expected

  10. 10

    numpy elementwise comparison with overloaded operator

  11. 11

    Rspec and Rails NoMethodError for comparison operator

  12. 12

    Operator associativity in prefix expression

  13. 13

    Android: Databinding expression with enum comparison

  14. 14

    Conditional statement, "unary operator expected"

  15. 15

    AJAX call is not working as expected

  16. 16

    jQuery .when().then() not working as expected

  17. 17

    .vimrc file not working as expected

  18. 18

    *ngIf not working as expected with observable

  19. 19

    ItemIsAutoTristate flag not working as expected

  20. 20

    KeyboardAvoidingView not working as expected on IOS

  21. 21

    PowerShell variables not working as expected

  22. 22

    XPath logical 'and' not working as expected?

  23. 23

    CancellationTokenSource not working as expected on a TaskCompletionSource

  24. 24

    string formatting not working as expected

  25. 25

    Why is .on() Not working as expected in jquery?

  26. 26

    Tuples in Scala not working as expected

  27. 27

    strpos() not working as expected

  28. 28

    PHP Dateformat not working as expected

  29. 29

    Update not working as expected

ホットタグ

アーカイブ