【发布时间】:2018-01-27 08:03:54
【问题描述】:
如果我有这样的表:
df = pd.DataFrame({
'hID': [101, 102, 103, 101, 102, 104, 105, 101],
'dID': [10, 11, 12, 10, 11, 10, 12, 10],
'uID': ['James', 'Henry', 'Abe', 'James', 'Henry', 'Brian', 'Claude', 'James'],
'mID': ['A', 'B', 'A', 'B', 'A', 'A', 'A', 'C']
})
我可以在 Qlik 中执行 count(distinct hID) 来计算 5 的唯一 HID。如何使用 pandas 数据框在 python 中做到这一点?或者也许是一个numpy数组?同样,如果要执行count(hID),我将在 Qlik 中获得 8。在 pandas 中等效的方法是什么?
【问题讨论】:
-
@piRSquared 谢谢。我可以做类似 df[['dID','hID']].agg(['count', 'size', 'nunique']) 之类的事情,它可以工作。但是与 groupby 结合使用时不起作用。所以 df[['dID','hID']].groupby('mID').agg(['count', 'size', 'nunique']) 说 KeyError。有没有办法选择特定的列并应用条件?
-
三种方式
df[['mID', 'dID','hID']].groupby('mID').agg(['count', 'size', 'nunique']) -
或
df[['dID','hID']].groupby(df['mID']).agg(['count', 'size', 'nunique']) -
或
df.groupby('mID')[['dID', 'hID']].agg(['count', 'size', 'nunique'])
标签: python pandas numpy qlikview qliksense