【问题标题】:Count null/NaN values in a dataframe across columns跨列计算数据框中的 null/NaN 值
【发布时间】:2015-09-23 04:47:50
【问题描述】:

我正在尝试计算数据框各列中每一行的唯一值的数量。

More context in my previous post and my answer

这是当前的数据框:

[in] df
[out] 
         PID         CID      PPID        PPPID       PPPPID        PPPPPID
    0   2015-01-02   456      2014-01-02  2014-01-02  2014-01-02    2014-01-02
    1   2015-02-02   500      2014-02-02  2013-02-02  2012-02-02    2012-02-10  
    2   2010-12-04   300      2010-12-04  2010-12-04  2010-12-04    2010-12-04 

除 CID (contract_ID) 之外的所有列都是日期时间。我想在数据框中添加另一列,用于计算每行中唯一日期时间的数量(目的是找出“链”中有多少合同)。

我尝试了 .count().sum() 方法的不同实现,但无法让它们逐行工作(输出是具有相同值的所有行)。

例子:

df_merged['COUNT'] = df_merged2.count(axis=1)

当我希望每一行都不同时,用“6”填充整个“COUNT”列。

删除 axis=1 参数会使整个列变为“NaN”

【问题讨论】:

    标签: python datetime pandas nan


    【解决方案1】:

    您需要apply(your_func, axis=1) 才能逐行工作。

    df
    
    Out[19]: 
              PID  CID        PPID       PPPID      PPPPID     PPPPPID
    0  2015-01-02  456  2014-01-02  2014-01-02  2014-01-02  2014-01-02
    1  2015-02-02  500  2014-02-02  2013-02-02  2012-02-02  2012-02-10
    2  2010-12-04  300  2010-12-04  2010-12-04  2010-12-04  2010-12-04
    
    
    
    df['counts'] = df.drop('CID', axis=1).apply(lambda row: len(pd.unique(row)), axis=1)
    
    Out[20]: 
              PID  CID        PPID       PPPID      PPPPID     PPPPPID  counts
    0  2015-01-02  456  2014-01-02  2014-01-02  2014-01-02  2014-01-02       2
    1  2015-02-02  500  2014-02-02  2013-02-02  2012-02-02  2012-02-10       5
    2  2010-12-04  300  2010-12-04  2010-12-04  2010-12-04  2010-12-04       1
    
    [3 rows x 7 columns]
    

    【讨论】:

      【解决方案2】:

      另一种方法是在 df 的转置上调用 unique

      In [26]:    
      df['counts'] = df.drop('CID', axis=1).T.apply(lambda x: len(pd.Series.unique(x)))
      df
      
      Out[26]:
                PID  CID        PPID       PPPID      PPPPID     PPPPPID  counts
      0  2015-01-02  456  2014-01-02  2014-01-02  2014-01-02  2014-01-02       2
      1  2015-02-02  500  2014-02-02  2013-02-02  2012-02-02  2012-02-10       5
      2  2010-12-04  300  2010-12-04  2010-12-04  2010-12-04  2010-12-04       1
      

      【讨论】:

        【解决方案3】:

        您可以直接在DataFrame 上使用nunique。这是从pd.__version__ == u'0.20.0' 开始的。

        In [169]: df['counts'] = df.drop('CID', axis=1).nunique(axis=1)
        
        In [170]: df
        Out[170]:
                  PID  CID        PPID       PPPID      PPPPID     PPPPPID  counts
        0  2015-01-02  456  2014-01-02  2014-01-02  2014-01-02  2014-01-02       2
        1  2015-02-02  500  2014-02-02  2013-02-02  2012-02-02  2012-02-10       5
        2  2010-12-04  300  2010-12-04  2010-12-04  2010-12-04  2010-12-04       1
        

        【讨论】:

          猜你喜欢
          • 2016-04-09
          • 2021-11-25
          • 2019-04-10
          • 2012-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-21
          • 1970-01-01
          相关资源
          最近更新 更多