0

I have the following function to change the value of column 'A' to np.NaN if the current value is '...'. However, for some reason, nothing is changing.

def findEmpty(row):
    row['A'] = np.NaN


energy[energy['A'] == '...'].apply(lambda x:findEmpty(x))

Can someone help?

3
  • Why don't you use fillna? pandas.pydata.org/pandas-docs/stable/generated/… Commented Nov 17, 2016 at 12:55
  • I am learning python, and I would like to know how to use lambda in this scenario. @anshanno Commented Nov 17, 2016 at 12:57
  • Can you post sample data? Commented Nov 17, 2016 at 13:01

3 Answers 3

2

Pandas apply returns a DataFrame with applied values. Try this:

energy = energy[energy['A'] == '...'].apply(lambda x:findEmpty(x))
Sign up to request clarification or add additional context in comments.

Comments

1

here is a much more efficient solution

energy.ix[(energy['A'] == '...'), 'A'] = np.NaN

be aware that lambda functions are in pure Python. In other words, you basically throw away all the nice C speed-ups in Pandas when you do so. If you use large datasets, always try to avoid loops and lambda functions (when possible)

Comments

1

Pandas has a 'replace' method which would improve the readability

energy.replace('...', np.NaN, inplace=True)

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.