<small id='BE9Vh'></small><noframes id='BE9Vh'>

    1. <legend id='BE9Vh'><style id='BE9Vh'><dir id='BE9Vh'><q id='BE9Vh'></q></dir></style></legend>

        <tfoot id='BE9Vh'></tfoot>
          <bdo id='BE9Vh'></bdo><ul id='BE9Vh'></ul>
      1. <i id='BE9Vh'><tr id='BE9Vh'><dt id='BE9Vh'><q id='BE9Vh'><span id='BE9Vh'><b id='BE9Vh'><form id='BE9Vh'><ins id='BE9Vh'></ins><ul id='BE9Vh'></ul><sub id='BE9Vh'></sub></form><legend id='BE9Vh'></legend><bdo id='BE9Vh'><pre id='BE9Vh'><center id='BE9Vh'></center></pre></bdo></b><th id='BE9Vh'></th></span></q></dt></tr></i><div id='BE9Vh'><tfoot id='BE9Vh'></tfoot><dl id='BE9Vh'><fieldset id='BE9Vh'></fieldset></dl></div>

        pandas python中的COUNTIF在具有多个条件的多个列上

        COUNTIF in pandas python over multiple columns with multiple conditions(pandas python中的COUNTIF在具有多个条件的多个列上)
        • <bdo id='axHRB'></bdo><ul id='axHRB'></ul>

            <i id='axHRB'><tr id='axHRB'><dt id='axHRB'><q id='axHRB'><span id='axHRB'><b id='axHRB'><form id='axHRB'><ins id='axHRB'></ins><ul id='axHRB'></ul><sub id='axHRB'></sub></form><legend id='axHRB'></legend><bdo id='axHRB'><pre id='axHRB'><center id='axHRB'></center></pre></bdo></b><th id='axHRB'></th></span></q></dt></tr></i><div id='axHRB'><tfoot id='axHRB'></tfoot><dl id='axHRB'><fieldset id='axHRB'></fieldset></dl></div>
              <tbody id='axHRB'></tbody>

                <legend id='axHRB'><style id='axHRB'><dir id='axHRB'><q id='axHRB'></q></dir></style></legend>

                <small id='axHRB'></small><noframes id='axHRB'>

                • <tfoot id='axHRB'></tfoot>
                • 本文介绍了pandas python中的COUNTIF在具有多个条件的多个列上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个数据集,我试图在其中确定每个人的风险因素数量.所以我有以下数据:

                  I have a dataset wherein I am trying to determine the number of risk factors per person. So I have the following data:

                  Person_ID  Age  Smoker  Diabetes
                        001   30       Y         N
                        002   45       N         N
                        003   27       N         Y
                        004   18       Y         Y
                        005   55       Y         Y
                  

                  每个属性(年龄、吸烟者、糖尿病)都有自己的条件来确定它是否是风险因素.因此,如果年龄 >= 45,这是一个风险因素.如果吸烟者和糖尿病是Y",则它们是危险因素.我想要添加一个列,根据这些条件将每个人的风险因素的数量加起来.所以数据看起来像这样:

                  Each attribute (Age, Smoker, Diabetes) has its own condition to determine whether it is a risk factor. So if Age >= 45, it's a risk factor. Smoker and Diabetes are risk factors if they are "Y". What I would like is to add a column that adds up the number of risk factors for each person based on those conditions. So the data would look like this:

                  Person_ID  Age  Smoker  Diabetes  Risk_Factors
                        001   30       Y         N             1
                        002   25       N         N             0
                        003   27       N         Y             1
                        004   18       Y         Y             2
                        005   55       Y         Y             3
                  

                  我在 Excel 中有一个示例数据集,我在其中使用的方法是使用 COUNTIF 公式,如下所示:

                  I have a sample dataset that I was fooling around with in Excel, and the way I did it there was to use the COUNTIF formula like so:

                  =COUNTIF(B2,">45") + COUNTIF(C2,"=Y") + COUNTIF(D2,"=Y")

                  但是,我将使用的实际数据集对于 Excel 来说太大了,所以我正在为 python 学习 pandas.我希望我能提供我已经尝试过的例子,但坦率地说,我什至不知道从哪里开始.我查看了 这个问题,但它并没有真正解决什么问题使用来自多个列的不同条件将其应用于整个新列.有什么建议吗?

                  However, the actual dataset that I will be using is way too large for Excel, so I'm learning pandas for python. I wish I could provide examples of what I've already tried, but frankly I don't even know where to start. I looked at this question, but it doesn't really address what to do about applying it to an entire new column using different conditions from multiple columns. Any suggestions?

                  推荐答案

                  如果你想坚持使用 pandas.您可以使用以下...

                  If you want to stick with pandas. You can use the following...

                  isY = lambda x:int(x=='Y')
                  countRiskFactors = lambda row: isY(row['Smoker']) + isY(row['Diabetes']) + int(row["Age"]>45)
                  
                  df['Risk_Factors'] = df.apply(countRiskFactors,axis=1)
                  

                  工作原理

                  isY - 是一个存储的 lambda 函数,用于检查单元格的值是否为 Y,否则返回 1,否则为 0countRiskFactors - 将风险因素相加

                  isY - is a stored lambda function that checks if the value of a cell is Y returns 1 if it is otherwise 0 countRiskFactors - adds up the risk factors

                  最后一行使用 apply 方法,参数键设置为 1,它将方法 -first 参数 - 沿 DataFrame 逐行应用,并返回一个附加到 DataFrame 的 Series.

                  the final line uses the apply method, with the paramater key set to 1, which applies the method -first parameter - row wise along the DataFrame and Returns a Series which is appended to the DataFrame.

                  打印 df 的输出

                     Person_ID  Age Smoker Diabetes  Risk_Factors
                  0          1   30      Y        N             1
                  1          2   45      N        N             0
                  2          3   27      N        Y             1
                  3          4   18      Y        Y             2
                  4          5   55      Y        Y             3
                  

                  这篇关于pandas python中的COUNTIF在具有多个条件的多个列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                      <tbody id='KVQAW'></tbody>

                    <small id='KVQAW'></small><noframes id='KVQAW'>

                      <legend id='KVQAW'><style id='KVQAW'><dir id='KVQAW'><q id='KVQAW'></q></dir></style></legend>
                      <i id='KVQAW'><tr id='KVQAW'><dt id='KVQAW'><q id='KVQAW'><span id='KVQAW'><b id='KVQAW'><form id='KVQAW'><ins id='KVQAW'></ins><ul id='KVQAW'></ul><sub id='KVQAW'></sub></form><legend id='KVQAW'></legend><bdo id='KVQAW'><pre id='KVQAW'><center id='KVQAW'></center></pre></bdo></b><th id='KVQAW'></th></span></q></dt></tr></i><div id='KVQAW'><tfoot id='KVQAW'></tfoot><dl id='KVQAW'><fieldset id='KVQAW'></fieldset></dl></div>

                        <bdo id='KVQAW'></bdo><ul id='KVQAW'></ul>
                            <tfoot id='KVQAW'></tfoot>