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.
NaNin there0.23