Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fill missing values with a tuple

consider df

np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice([(1, 2), (3, 4), np.nan], (10, 10)))
df

enter image description here

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))

enter image description here

like image 208
piRSquared Avatar asked Dec 15 '16 00:12

piRSquared


2 Answers

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)
like image 116
Abdou Avatar answered Nov 16 '22 11:11

Abdou


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)
like image 33
3novak Avatar answered Nov 16 '22 11:11

3novak