I have a dataframe in pandas that stores a column containing ratios. The ratios need to be transformed into a log2 scale for plotting but the ratio values are often 0, leading in log2(0) which is recorded as inf or a missing value in pandas. I want to visualize these since in my dataframe a ratio value of 0 is meaningful. What is the best way to deal with this in pandas/numpy? When I take the log values, is the preferred way to do this?
# take log with tiny value added c = 0.0000001 df[col].apply(lamda x: log2(c + x)) or are there other ways? thanks.
infs with a completely arbitrary numberlog2(.0000001)Why don't you just remove theinfs when you plot and leave the0s when not plotting?c = 1.0thenlog2(c + x)will map [0,inf) --> [0,inf).0in a log plot (which should be at $\infty$, you could consider thepyplot.symlogfunction.