I am doing division of column A by column B. Certain places where denominator is 0, I get inf values. How do I replace these inf values with 1 from column C?
Df['C'] = Df['A'] / Df['B']
I am doing division of column A by column B. Certain places where denominator is 0, I get inf values. How do I replace these inf values with 1 from column C?
Df['C'] = Df['A'] / Df['B']
Try the following:
import numpy as np
Df = Df[Df.C != np.inf]
Edit:
To replace all np.inf
values by 1, use this code:
Df.loc[Df.C == np.inf] = 1
Alternatively, you can use pandas.DataFrame.replace()
method like this:
Df['C'] = Df['C'].replace({np.inf: 1})
df['C'] = df['A'].div(df['B']) # div will replace inf with null
filt = df['C'].notna() # a filter for not null
df = df.loc[filt] # the new dataframe with no null