user248237
user248237

Reputation:

dealing with zero log values in numpy/pandas

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.

Upvotes: 2

Views: 3354

Answers (1)

user1987630
user1987630

Reputation:

I guess you can use numpy.inf to identify those that are infinity and treat them separately.

Ref: github.com/pydata/pandas

Upvotes: 3

Related Questions