TransWikia.com

Pandas - handle inf and NaN in one line

Stack Overflow Asked by 8-Bit Borges on December 3, 2021

I’m trying to get rid of both inf and NaNs in my series.

If I do:

df['Iprod'] = df['PD']/df['PF'].replace(np.inf, 0).fillna(0)

Inf is gone, but not NaN.

Is there a one liner to fill NaN as well?

2 Answers

df.replace([np.nan, np.inf, -np.inf], 0, inplace=True)

Answered by Harshwardhan Nandedkar on December 3, 2021

Adding ()

df['Iprod'] = (df['PD']/df['PF']).replace(np.inf, 0).fillna(0)

Answered by BENY on December 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP