Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas opposite of fillna(0)

Whereas df.fillna(0) fills all NA/NaN values with 0, is there a function to replace all non-NA/NaN values with another value, such as 1?

If the values in my DataFrame are variable-length lists then:

  • df.replace() requires that the lists are the same length
  • boolean index like df[len(df) > 0] = 1 throws ValueError: cannot insert True, already exists
  • pandas.get_dummies() throws TypeError: unhashable type: 'list'

Is there a more straightforward solution?

like image 497
interpolack Avatar asked Jul 29 '15 19:07

interpolack


People also ask

What does Fillna 0 do in Python?

The fillna() function is used to fill NA/NaN values using the specified method. Value to use to fill holes (e.g. 0), alternately a dict/Series/DataFrame of values specifying which value to use for each index (for a Series) or column (for a DataFrame). Values not in the dict/Series/DataFrame will not be filled.

Is Fillna a panda?

Pandas DataFrame fillna() MethodThe fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.

How do I forward NaN values in pandas?

You can use pandas. DataFrame. fillna with the method='ffill' option. 'ffill' stands for 'forward fill' and will propagate last valid observation forward.


2 Answers

You could use indexing/assignment with df[df.notnull()] = 1. For instance:

>>> df = pd.DataFrame([[np.nan, 2, 5], [2, 5, np.nan], [2, 5, np.nan]])
>>> df # example frame
    0  1   2
0 NaN  2   5
1   2  5 NaN
2   2  5 NaN

>>> df[df.notnull()] = 1
>>> df
    0  1   2
0 NaN  1   1
1   1  1 NaN
2   1  1 NaN
like image 127
Alex Riley Avatar answered Oct 19 '22 10:10

Alex Riley


I don't know of a built-in function, but this works:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'a':[np.nan, 13, 32]})

 >>    a
   0  NaN
   1   13
   2   32

df = df.applymap(lambda x: 1 if not np.isnan(x) else x)

 >>     a
    0 NaN
    1   1
    2   1
like image 31
DeepSpace Avatar answered Oct 19 '22 11:10

DeepSpace