Reputation: 21
I have this pandas dataframe:
id A B
1 nan 0
2 nan 1
3 6 0
4 nan 1
5 12 1
6 14 0
I want to change the value of all nan is 'A' based on the value of 'B', for example if B = 0, A should be random number between [0,1] if B = 1, A should be random number between [1,3]
How do i do this?
Upvotes: 2
Views: 70
Reputation: 863741
Solution if performance is important - generate random values by length of DataFrame and then assign values by conditions:
Use numpy.random.randint
for generate random values and pass to numpy.select
with chainded condition with &
for bitwise AND, compare is by Series.isna
and Series.eq
:
a = np.random.randint(0,2, size=len(df)) #generate 0,1
b = np.random.randint(1,4, size=len(df)) #generate 1,2,3
m1 = df.A.isna()
m2 = df.B.eq(0)
m3 = df.B.eq(1)
df['A'] = np.select([m1 & m2, m1 & m3],[a, b], df.A)
print (df)
id A B
0 1 1.0 0
1 2 3.0 1
2 3 6.0 0
3 4 3.0 1
4 5 12.0 1
5 6 14.0 0
Upvotes: 2