【问题标题】:Assigning values to Variables based on Conditions根据条件为变量赋值
【发布时间】:2020-02-18 12:25:18
【问题描述】:

如果值是数字,我想在 pandas 数据框中找到列的平均值,如果值是分类值,我想找到系列的模式。我只想使用我称之为“meanmode”的一个变量来做到这一点。

当我尝试以下操作时:

def mean_mode(val):
   return meanmode = val.mean() if val.dtype != 'object' else val.mode()[0]

我得到错误:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我如何为变量“meanmode”分配其各自的平均值(如果是数值)和模式(如果是分类)?

到目前为止我的代码:

def report(val):
    dtypes = val.dtypes
    rows = val.T.apply(lambda x: x.count(), axis=1)
    nuniq = val.T.apply(lambda x: x.nunique() , axis=1)
    uniq = val.T.apply(lambda x: x.unique() if x.dtype == 'object' else None, axis=1)
    total = val.T.apply(lambda x: x.isna().sum(), axis=1)
    count = val.shape[0]
    pc = np.round(total / count * 100, 2)

    mini = val.min()
    maxi = val.max()

    meanmode = val.apply(lambda x: x.mode()[0] if x.dtype == 'object' else mean(val))

    qualitydf = pd.concat([dtypes, rows, total, pc, meanmode, mini, maxi, nuniq, uniq],
                          keys=['Dtype', 'Available Rows', 'Missing Values',
                                'Percent Missing', 'Mean-Mode',
                                'Min', 'Max', 
                                'No. Of Uniques', 'Unique Values'], axis=1)

return qualitydf

【问题讨论】:

  • val 是数据框、系列还是什么?
  • Val 是一个数据框。从该数据帧中,此代码提取要在 qualitydf(输出数据帧)中显示给用户的特征。虽然所有代码都可以正常工作,但我被困在 meanmode 问题上。

标签: python pandas variables mean mode


【解决方案1】:

使用DataFrame.pipe:

df = pd.DataFrame({
        'A':list('abccef'),
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':list('baabbb')
})

def report(val):
    dtypes = val.dtypes
    rows = val.T.apply(lambda x: x.count(), axis=1)
    nuniq = val.T.apply(lambda x: x.nunique() , axis=1)
    uniq = val.T.apply(lambda x: x.unique() if x.dtype == 'object' else None, axis=1)
    total = val.T.apply(lambda x: x.isna().sum(), axis=1)
    count = val.shape[0]
    pc = np.round(total / count * 100, 2)

    mini = val.min()
    maxi = val.max()

    meanmode = val.apply(lambda x: x.mode()[0] if x.dtype == 'object' else mean(val))

    qualitydf = pd.concat([dtypes, rows, total, pc, meanmode, mini, maxi, nuniq, uniq],
                          keys=['Dtype', 'Available Rows', 'Missing Values',
                                'Percent Missing', 'Mean-Mode',
                                'Min', 'Max', 
                                'No. Of Uniques', 'Unique Values'], axis=1)

    return qualitydf

df = df.pipe(report)
print(df)
    Dtype  Available Rows  Missing Values  Percent Missing Mean-Mode Min Max  \
A  object               6               0              0.0         c   a   f   
B   int64               6               0              0.0         4   4   5   
C   int64               6               0              0.0         2   2   9   
D   int64               6               0              0.0         1   0   7   
E   int64               6               0              0.0         2   2   9   
F  object               6               0              0.0         b   a   b   

   No. Of Uniques       Unique Values  
A               5     [a, b, c, e, f]  
B               2              [4, 5]  
C               6  [7, 8, 9, 4, 2, 3]  
D               5     [1, 3, 5, 7, 0]  
E               6  [5, 3, 6, 9, 2, 4]  
F               2              [b, a]  

【讨论】:

  • 我基本上是在制作数据集的质量报告。它向用户显示其平均值、最大值、最小值、唯一性和数据类型。虽然其余的都很好,但我似乎无法将均值和模式分配给它的同一个变量。
  • 现在 min() 值和 mean() 值相同。但是 mode() 工作正常。
猜你喜欢
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
相关资源
最近更新 更多