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

      <small id='46tYl'></small><noframes id='46tYl'>

      优雅地编写np.WHERE表示列中的不同值

      Elegant way to write np.where for different values in a column(优雅地编写np.WHERE表示列中的不同值)
      <i id='WM58D'><tr id='WM58D'><dt id='WM58D'><q id='WM58D'><span id='WM58D'><b id='WM58D'><form id='WM58D'><ins id='WM58D'></ins><ul id='WM58D'></ul><sub id='WM58D'></sub></form><legend id='WM58D'></legend><bdo id='WM58D'><pre id='WM58D'><center id='WM58D'></center></pre></bdo></b><th id='WM58D'></th></span></q></dt></tr></i><div id='WM58D'><tfoot id='WM58D'></tfoot><dl id='WM58D'><fieldset id='WM58D'></fieldset></dl></div>

        • <legend id='WM58D'><style id='WM58D'><dir id='WM58D'><q id='WM58D'></q></dir></style></legend><tfoot id='WM58D'></tfoot>
          • <bdo id='WM58D'></bdo><ul id='WM58D'></ul>

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

                <tbody id='WM58D'></tbody>

              • 本文介绍了优雅地编写np.WHERE表示列中的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有如下所示的数据帧

                df = pd.DataFrame({'person_id': [101,101,101,101,202,202,202],
                                   'person_type':['A','A','B','C','D','B','A'],
                                   'login_date':['5/7/2013 09:27:00 AM','09/08/2013 11:21:00 AM','06/06/2014 08:00:00 AM','06/06/2014 05:00:00 AM','12/11/2011 10:00:00 AM','13/10/2012 12:00:00 AM','13/12/2012 11:45:00 AM'],
                                   'logout_date':[np.nan,'11/08/2013 11:21:00 AM',np.nan,'06/06/2014 05:00:00 AM',np.nan,'13/10/2012 12:00:00 AM',np.nan]})
                df.login_date = pd.to_datetime(df.login_date)
                df.logout_date = pd.to_datetime(df.logout_date)
                

                我要将2个规则应用到logout_date

                规则1-如果人员类型为BCDE且logout_date为NaN,则复制登录日期值

                规则2-如果人员类型为A且logout_date为NaN,则将登录日期增加2天

                我尝试了下面的

                df['logout_date'] = np.where(((df['person_type'].isin(['B','C','D'])) & (df['logout_date'].isna())),df['login_date'].dt.date,df['logout_date'].dt.date)
                df['logout_date'] = np.where(((df['person_type'].isin(['A'])) & (df['logout_date'].isna())),df['login_date'] + pd.DateOffset(days=2).dt.date,df['logout_date'].dt.date)
                

                你可以看到它有多长。有没有其他更好的方法来写这篇文章?

                我希望我的输出如下所示

                person_id   person_type login_date           logout_date
                101            A        2013-05-07 09:27:00  2013-05-09 09:27:00
                101            A        2013-09-08 11:21:00  2013-11-08 11:21:00
                101            B        2014-06-06 08:00:00  2014-06-06 08:00:00
                101            C        2014-06-06 05:00:00  2014-06-06 05:00:00
                202            D        2011-12-11 10:00:00  2011-12-11 10:00:00
                202            B        2012-10-13 00:00:00  2012-10-13 12:00:00
                202            A        2012-12-13 11:45:00  2012-12-15 11:45:00
                

                推荐答案

                对注释中提到的中间变量使用numpy.select

                s = df['person_type'].fillna('missing value')
                m1 = s.isin(['B','C','D', 'missing value'])
                m2 = s.isin(['A','missing value'])
                
                df['logout_date'] = np.select([m1, m2],
                                              [df['login_date'], df['login_date'] + pd.DateOffset(days=2)],
                                               default=df['logout_date'])
                

                或重写您的解决方案:

                m1 = df['person_type'].isin(['B','C','D'])
                m2 = df['person_type'].isin(['A'])
                m3 = df['logout_date'].isna()
                
                df['logout_date'] = np.select([m1 & m3, m2 & m3],
                                              [df['login_date'], df['login_date'] + pd.DateOffset(days=2)],
                                               default=df['logout_date'])
                

                df['logout_date'] = np.select([m1 & m3, m2 & m3],
                                              [df['login_date'].dt.date, 
                                               (df['login_date'] + pd.DateOffset(days=2)).dt.date],
                                               default=df['logout_date'].dt.date)
                

                这篇关于优雅地编写np.WHERE表示列中的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                <i id='KD8nG'><tr id='KD8nG'><dt id='KD8nG'><q id='KD8nG'><span id='KD8nG'><b id='KD8nG'><form id='KD8nG'><ins id='KD8nG'></ins><ul id='KD8nG'></ul><sub id='KD8nG'></sub></form><legend id='KD8nG'></legend><bdo id='KD8nG'><pre id='KD8nG'><center id='KD8nG'></center></pre></bdo></b><th id='KD8nG'></th></span></q></dt></tr></i><div id='KD8nG'><tfoot id='KD8nG'></tfoot><dl id='KD8nG'><fieldset id='KD8nG'></fieldset></dl></div>
                1. <small id='KD8nG'></small><noframes id='KD8nG'>

                      <tbody id='KD8nG'></tbody>
                      <tfoot id='KD8nG'></tfoot>

                      <legend id='KD8nG'><style id='KD8nG'><dir id='KD8nG'><q id='KD8nG'></q></dir></style></legend>
                        <bdo id='KD8nG'></bdo><ul id='KD8nG'></ul>