accessing nan values from array of arays

Viewed 63

I'm trying to find where nans are in this column, change it to a different value and update the dataset. I was able to access the nan by using [0][0] for this example but this solution will not work if I don't know where the nans are. I'm hoping someone can suggest how can we dynamically search for nans and change them when they happen to be in array of arrays.

def check_for_nan():
    data = {'numbers': [[np.nan], [11, 14], [45, 56], [67, 88], [99]]}
    df = pd.DataFrame(data=data)
    df["numbers"][0][0] = 40
    print(df["numbers"])

Desired Output:

0        [40]
1    [11, 14]
2    [45, 56]
3    [67, 88]
4        [99]
Name: numbers, dtype: object
1 Answers

You can try: Expands ['numbers'] into separate columns

df=df['numbers'].apply(pd.Series)

Locates rows with nans

df_nan=df[df.isna().any(axis=1)]

Assign values to specific nan cells:

df_nan.at[0,0]=40
df_nan.at[4,1]=41

Place values back in original data frame

df=df.fillna(df_nan)

Back to original format

df['combined']=df.stack().groupby(level=0).apply(list).tolist()
Related