[英]Change values of one column based on values of other column pandas dataframe
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]我想根据'B'的值改变所有nan的值是'A',例如如果B = 0,A应该是[0,1]之间的随机数如果B = 1,A应该是随机数[1,3]之间
How do i do this?我该怎么做呢?
Solution if performance is important - generate random values by length of DataFrame and then assign values by conditions:如果性能很重要,解决方案 - 按 DataFrame 的长度生成随机值,然后按条件分配值:
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
:使用
numpy.random.randint
生成随机值并通过链接条件传递给numpy.select
并使用&
进行位与,比较是通过Series.isna
和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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.