1

Let's take as an example with this simple dataset:

example_set  = pd.DataFrame(data = {"dirr":[1,0,-1,-1,1,-1,0], 
                                    "value": [125,130,80,8,150,251,18], 
                                    "result":[np.NaN for _ in range(7)]})

The following line returns error:invalid syntax

example_set["result"].apply(lambda x : example_set["value"]if x["dirr"]==1)

Can anyone tell me what I am doing wrong? Please do not come up with solution of how to do it without lambda: This is only a super simplified example that I made up to isolate my problem.

3
  • @EdChum Please read the question fully: Please do not come up with solution of how to do it without lambdas: This is only a super simplified example that I made up to isolate my problem. Commented Jan 2, 2017 at 16:04
  • .apply(lambda x : example_set["value"] if x["dirr"]==1 else x)? Commented Jan 2, 2017 at 16:06
  • @MYGz this returns another error: TypeError: 'float' object is not subscriptable Commented Jan 2, 2017 at 16:07

1 Answer 1

8

You need to include the else return value in the lambda statement:

In [7]:
example_set['result'] = example_set.apply(lambda x: x['value'] if x['dirr'] == 1 else x['result'], axis = 1)
example_set

Out[7]:
   dirr  result  value
0     1   125.0    125
1     0     NaN    130
2    -1     NaN     80
3    -1     NaN      8
4     1   150.0    150
5    -1     NaN    251
6     0     NaN     18

Your attempt:

example_set["result"].apply(lambda x : example_set["value"]if x["dirr"]==1)

lacked the else return value in essence, also you needed x['value'] not the whole df, using the whole df yields a strange and undesirable return result even if the statement was corrected:

In [14]:
example_set.apply(lambda x: example_set['value'] if x['dirr'] == 1 else example_set['result'], axis = 1)

Out[14]:
       0      1     2    3      4      5     6
0  125.0  130.0  80.0  8.0  150.0  251.0  18.0
1    NaN    NaN   NaN  NaN    NaN    NaN   NaN
2    NaN    NaN   NaN  NaN    NaN    NaN   NaN
3    NaN    NaN   NaN  NaN    NaN    NaN   NaN
4  125.0  130.0  80.0  8.0  150.0  251.0  18.0
5    NaN    NaN   NaN  NaN    NaN    NaN   NaN
6    NaN    NaN   NaN  NaN    NaN    NaN   NaN
Sign up to request clarification or add additional context in comments.

2 Comments

Also, you can't access corresponding values of other columns if you .apply() on a single column. True?
@MYGz yes that's correct, by definition it would call the Series version which has no axis param, only the DataFrame.apply version has axis param

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.