2

This is my data set

fake_abalone2

   Sex   Length  Diameter  Height   Whole  Shucked  Viscera  Shell  Rings
                                    Weight  Weight  Weight  Weight
0   M    0.455    0.365    0.095    0.5140  0.2245  0.1010  0.1500  15
1   M    0.350    0.265    0.090    0.2255  0.0995  0.0485  0.0700  7
2   F    0.530    0.420    0.135    0.6770  0.2565  0.1415  0.2100  9
3   M    0.440    0.365    0.125    0.5160  0.2155  0.1140  0.1550  10
4   K    0.330    0.255    0.080    0.2050  0.0895  0.0395  0.0550  7
5   K    0.425    0.300    0.095    0.3515  0.1410  0.0775  0.1200  8

Getting syntax error while using the following method. Please help me out. I want the value in "sex" table to change depending on "Rings" table.If "Rings" value is less than 10 the corresponding "sex" value should be changed to 'K'.Otherwise, no change should be made in "Sex" table.

 fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)

File "", line 1 fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)

SyntaxError: invalid syntax

3
  • 2
    Welcome to StackOverflow! Please format your question, don't forget to read the How to ask guide. Commented Mar 16, 2018 at 23:20
  • What should be the output of the lambda if x >= 10? Commented Mar 17, 2018 at 1:35
  • for x>=10 there sholud not be any change in column "Sex" Commented Mar 17, 2018 at 4:22

3 Answers 3

3

The Following method works perfectly.

    df1["Sex"]=df1.apply(lambda x: "K"if x.Rings<10 else x["Sex"],axis=1)

df1 is the dataframe

  Sex   Length  Diameter Height Whole   Shucked Viscera Shell Rings         
                                weight  weight  weight  weight
0   M   0.455   0.365   0.095   0.5140  0.2245  0.1010  0.1500  15
1   K   0.350   0.265   0.090   0.2255  0.0995  0.0485  0.0700  7
2   K   0.530   0.420   0.135   0.6770  0.2565  0.1415  0.2100  9
3   M   0.440   0.365   0.125   0.5160  0.2155  0.1140  0.1550  10
4   K   0.330   0.255   0.080   0.2050  0.0895  0.0395  0.0550  7
5   K   0.425   0.300   0.095   0.3515  0.1410  0.0775  0.1200  8
6   F   0.530   0.415   0.150   0.7775  0.2370  0.1415  0.3300  20
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Python numpy instead of lambda function.

Import python numpy using import numpy as np

then you can use the following method to replace the string.

fake_abalone2['Sex'] = np.where(fake_abalone2['Rings']<10, 'K', fake_abalone2['Sex']) 

1 Comment

This method works well.But, is it wise to load Numpy Library just to use where method?
0

The main problem is the output of the lambda function:

.apply(lambda x:"K" if x<10)

The output is not certain for other conditions, so you can use else something ...

.apply(lambda x:"K" if x<10 else None)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.