1

How can I apply numpy log function to a panda dataframe that has 0 values in it?

This is an example of the series inside the dataframe I want to transform

df = pd.DataFrame({'CoapplicantIncome': [0, 1508.0, 0, 2358.0, 0], 'ApplicantIncome': [5849, 4583, 3000, 2583, 6000]}) ApplicantIncome CoapplicantIncome 0 5849 0.0 1 4583 1508.0 2 3000 0.0 3 2583 2358.0 4 6000 0.0 

I tried to use

df['CoapplicantIncome'] = np.log(df['CoapplicantIncome']) 

but it says 'cannot convert float NaN to integer'

After I do some searching, I've come to this line of code that says it will handle the 0 values.

df['CoapplicantIncome'] = np.log(df['CoapplicantIncome'].replace(0, np.nan)) 

But it still says 'cannot convert float NaN to integer'. So how can I solve this?

Interestingly, if I build this dataframe from scratch using pd.DataFramethe problem does not occur, but if I read the dataframe from a csv file, then the problem occur.

7
  • Cannot reproduce. Please make a stackoverflow.com/help/mcve Commented Jul 21, 2018 at 11:30
  • 1
    @qräbnö edited with additional information Commented Jul 21, 2018 at 11:41
  • 1
    Same pandas version as me. Your issue isn't reproducible from your example. Presumably you have a NaN in there Commented Jul 21, 2018 at 11:47
  • 2
    Throws a warning but runs ok in pandas 0.23 Commented Jul 21, 2018 at 11:47
  • 1
    @RafaelC ah right, cheers, I updated my pandas and it works now. Thanks Commented Jul 21, 2018 at 11:56

1 Answer 1

2

Fixed this problem by updating my pandas version.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.