I have this simple function with 2 columns. What I’m trying to do is to check what group has a number of nan and change it to a new desired value. Here’s a code snippet:
JavaScript
x
9
1
def twod_array():
2
data = {"group": [-1, 0, 1, 2, 3],
3
'numbers': [[2], [14, 15], [16, 17], [19, 20, 21], [np.nan]],
4
}
5
df = pd.DataFrame(data=data)
6
new_group_number = 100
7
df.loc[4, "group"] = new_group_number
8
return df
9
Before: This is how the data looks like, you can assume numbers are sorted.
JavaScript
1
7
1
group numbers
2
0 -1 [2]
3
1 0 [14, 15]
4
2 1 [16, 17]
5
3 2 [19, 20, 21]
6
4 3 [nan]
7
In my example I know where nan and since it was at position 4, I was able to use loc to change it to a 100, like this:
JavaScript
1
7
1
group numbers
2
0 -1 [2]
3
1 0 [14, 15]
4
2 1 [16, 17]
5
3 2 [19, 20, 21]
6
4 100 [nan]
7
What if I don’t know where the nan is? How can I know which group to update? All that comes to my mind is nested for loop which I would rather avoid… Any suggestions here?
Advertisement
Answer
You could replace
JavaScript
1
2
1
df.loc[4, "group"] = new_group_number
2
with
JavaScript
1
3
1
idx = df.numbers.apply(lambda l: any(pd.isna(e) for e in l))
2
df.loc[idx, 'group'] = new_group_number
3