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 lengthdf[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?
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.
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.
You can use pandas. DataFrame. fillna with the method='ffill' option. 'ffill' stands for 'forward fill' and will propagate last valid observation forward.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With