简体   繁体   English

根据python中的条件使用第1行或第2行的值更新数据框列

[英]Update dataframe column based with value from either row 1 or row 2 based on condition in python

I have a dataframe and want to create a column based on a condition that populates the row with the value of a row in another column.我有一个数据框,想根据条件创建一个列,该条件用另一列中的行值填充该行。

df = pd.DataFrame({'parent':[32, 3, 88, 9, 10, 23, 99, 23],
                   'id':[1, 2, 3, 4, 5, 6, 7, 8],
                   'flag':[True,True,False,True,False,True,True,True]})

I have tried to do this using np.where() but it doesn't update the value row by row but instead replaces all values within the column with the condition that is met.我尝试使用 np.where() 执行此操作,但它不会逐行更新值,而是使用满足的条件替换列中的所有值。

df['res'] = np.where(df['flag'] == True, df['parent'], df['id'])

The dataframe I want to create looks as follows:我要创建的数据框如下所示:

df = pd.DataFrame({'parent':[32, 3, 88, 9, 10, 23, 99, 23],
                   'id':[1, 2, 3, 4, 5, 6, 7, 8],
                   'flag':[True,True,False,True,False,True,True,True],
                   'res':[32, 3, 3, 9, 5, 23, 99, 23]})

Any ideas what I'm doing wrong?任何想法我做错了什么? I'm new to python, so any help is much appreciated.我是 python 的新手,所以非常感谢任何帮助。

Just change this:只需改变这个:

df['res'] = np.where(df['flag'] == True, output['parent'], output['id'])

to this:对此:

df['res'] = np.where(df['flag'] == True, df['parent'], df['id'])

Fix your code change the output to df修复您的代码将输出更改为 df

df['res1'] = np.where(df['flag'] == True, df['parent'], df['id'])
df
Out[176]: 
   parent  id   flag  res  res1
0      32   1   True   32    32
1       3   2   True    3     3
2      88   3  False    3     3
3       9   4   True    9     9
4      10   5  False    5     5
5      23   6   True   23    23
6      99   7   True   99    99
7      23   8   True   23    23

As others have pointed out, you had a typo in your code.正如其他人指出的那样,您的代码中有一个错字。 An alternative way the achieve the desired output is to use the apply method:实现所需输出的另一种方法是使用apply方法:

df['res'] = df.apply(lambda x : x['parent'] if x['flag'] else x['id'], 1)

Or或者

df['res'] = np.where(df['flag'], df['parent'], df['id'])

Output :输出

   parent  id   flag  res
0      32   1   True   32
1       3   2   True    3
2      88   3  False    3
3       9   4   True    9
4      10   5  False    5
5      23   6   True   23
6      99   7   True   99
7      23   8   True   23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM