Remove nan, +inf, -inf values columns from a dataframe

Viewed 332

How can you remove columns with NaN, +inf, and -inf values? All solutions I see here, mention only rows, but imagine I have a set of features from each element in the dataframe, how can I remove only the columns that contain +inf, -inf and NaN from each row in the dataframe?

1 Answers

First replace() inf and -inf with nan:

df = pd.DataFrame({'a':[1,2,3], 'b':[4,np.nan,6], 'c':[7,8,np.inf]})
df = df.replace([np.inf, -np.inf], np.nan)

#    a    b    c
# 0  1  4.0  7.0
# 1  2  NaN  8.0
# 2  3  6.0  NaN

Then use the axis param of dropna() to switch between row- and column-based behavior:

df.dropna() # default axis=0 is row-based

#    a    b    c
# 0  1  4.0  7.0
df.dropna(axis=1) # axis=1 or axis='columns' is column-based

#    a
# 0  1
# 1  2
# 2  3
Related