consider df
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice([(1, 2), (3, 4), np.nan], (10, 10)))
df
how do I fill those NaN
with (0, 0)
?
I've put this hack together but I'm assuming there is a more direct way. And this doesn't work for a pd.Series
df.stack().unstack(fill_value=(0, 0))
You can do with .applymap
:
import numpy as np
import pandas as pd
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice([(1, 2), (3, 4), np.nan], (10, 10)))
df.applymap(lambda x: (0,0) if x is np.nan else x)
This will work for a pd.Series
if you use apply
:
df[0].apply(lambda x: (0, 0) if x is np.nan else x)
I like your workaround better than this, but it should get the job done.
import pandas as pd
import numpy as np
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice([(1, 2), (3, 4), np.nan], (10, 10)))
idx_arrays = np.where(df.isnull())
idx_tups = zip(idx_arrays[0], idx_arrays[1])
for tup in idx_tups:
df.loc[tup] = (0, 0)
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