Pandas,基于列值的条件列分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38643012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:41:25  来源:igfitidea点击:

Pandas, conditional column assignment based on column values

pythonpandasnumpydataframe

提问by CentAu

How can I have conditional assignment in pandas by based on the values of two columns? Conceptually something like the following:

如何根据两列的值在Pandas中进行条件分配?从概念上讲,类似于以下内容:

Column_D = Column_B / (Column_B + Column_C) if Column_C is not null else Column_C

Concrete example:

具体例子:

import pandas as pd
import numpy as np
df = pd.DataFrame({'b': [2,np.nan,4,2,np.nan], 'c':[np.nan,1,2,np.nan,np.nan]})


     b    c
0  2.0  NaN
1  NaN  1.0
2  4.0  2.0
3  2.0  NaN
4  NaN  NaN

I want to have a new column dwhose result is division of column bby sum of band c, if cis not null, otherwise the value should be the value at column c. Something conceptually like the following:

我想有一个新的列d,其结果是列司b通过之bc,如果c不为空,否则这个数值应该在列中的值c。概念上类似于以下内容:

df['d'] = df['b']/(df['b']+df['c']) if not df['c'].isnull() else df['c']

desired result:

想要的结果:

     b    c         d
0  2.0  NaN       NaN
1  NaN  1.0       1.0
2  4.0  2.0       0.66
3  2.0  NaN       NaN
4  NaN  NaN       NaN

How can I achieve this?

我怎样才能做到这一点?

回答by MaxU

try this (if you want to have your desired result set - checking bcolumn):

试试这个(如果你想得到你想要的结果集 - 检查b列):

In [30]: df['d'] = np.where(df.b.notnull(), df.b/(df.b+df.c), df.c)

In [31]: df
Out[31]:
     b    c         d
0  2.0  NaN       NaN
1  NaN  1.0  1.000000
2  4.0  2.0  0.666667
3  2.0  NaN       NaN
4  NaN  NaN       NaN

or this, checking ccolumn:

或者这个,检查c列:

In [32]: df['d'] = np.where(df.c.notnull(), df.b/(df.b+df.c), df.c)

In [33]: df
Out[33]:
     b    c         d
0  2.0  NaN       NaN
1  NaN  1.0       NaN
2  4.0  2.0  0.666667
3  2.0  NaN       NaN
4  NaN  NaN       NaN