问题:像Qlik中那样在pandas数据框中的列中计算唯一值?

如果我有这样的表:

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']
})

我可以count(distinct hID)在Qlik中提出5个唯一的hID。我该如何在Python中使用Pandas数据框?还是一个numpy数组?同样,如果这样做,count(hID)我将在Qlik中得到8。在大熊猫中做这件事的等效方法是什么?

If I have a table like this:

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']
})

I can do count(distinct hID) in Qlik to come up with count of 5 for unique hID. How do I do that in python using a pandas dataframe? Or maybe a numpy array? Similarly, if were to do count(hID) I will get 8 in Qlik. What is the equivalent way to do it in pandas?


回答 0

计算不同的值,使用nunique

df['hID'].nunique()
5

仅计算非空值,请使用count

df['hID'].count()
8

计算包括空值在内的总值,请使用size属性:

df['hID'].size
8

编辑添加条件

使用布尔索引:

df.loc[df['mID']=='A','hID'].agg(['nunique','count','size'])

或使用query

df.query('mID == "A"')['hID'].agg(['nunique','count','size'])

输出:

nunique    5
count      5
size       5
Name: hID, dtype: int64

Count distinct values, use nunique:

df['hID'].nunique()
5

Count only non-null values, use count:

df['hID'].count()
8

Count total values including null values, use the size attribute:

df['hID'].size
8

Edit to add condition

Use boolean indexing:

df.loc[df['mID']=='A','hID'].agg(['nunique','count','size'])

OR using query:

df.query('mID == "A"')['hID'].agg(['nunique','count','size'])

Output:

nunique    5
count      5
size       5
Name: hID, dtype: int64

回答 1

如果我假设data是您数据框的名称,则可以执行以下操作:

data['race'].value_counts()

这将向您显示不同的元素及其发生的次数。

If I assume data is the name of your dataframe, you can do :

data['race'].value_counts()

this will show you the distinct element and their number of occurence.


回答 2

或获取每一列的唯一值数量:

df.nunique()

dID    3
hID    5
mID    3
uID    5
dtype: int64

新进 pandas 0.20.0 pd.DataFrame.agg

df.agg(['count', 'size', 'nunique'])

         dID  hID  mID  uID
count      8    8    8    8
size       8    8    8    8
nunique    3    5    3    5

您始终能够agg在内完成groupbystack最后使用了,因为我更喜欢演示文稿。

df.groupby('mID').agg(['count', 'size', 'nunique']).stack()


             dID  hID  uID
mID                       
A   count      5    5    5
    size       5    5    5
    nunique    3    5    5
B   count      2    2    2
    size       2    2    2
    nunique    2    2    2
C   count      1    1    1
    size       1    1    1
    nunique    1    1    1

Or get the number of unique values for each column:

df.nunique()

dID    3
hID    5
mID    3
uID    5
dtype: int64

New in pandas 0.20.0 pd.DataFrame.agg

df.agg(['count', 'size', 'nunique'])

         dID  hID  mID  uID
count      8    8    8    8
size       8    8    8    8
nunique    3    5    3    5

You’ve always been able to do an agg within a groupby. I used stack at the end because I like the presentation better.

df.groupby('mID').agg(['count', 'size', 'nunique']).stack()


             dID  hID  uID
mID                       
A   count      5    5    5
    size       5    5    5
    nunique    3    5    5
B   count      2    2    2
    size       2    2    2
    nunique    2    2    2
C   count      1    1    1
    size       1    1    1
    nunique    1    1    1

回答 3

您可以nunique在大熊猫中使用:

df.hID.nunique()
# 5

You can use nunique in pandas:

df.hID.nunique()
# 5

回答 4

要计算hIDdataframe列中的唯一值df,请使用:

len(df.hID.unique())

To count unique values in column, say hID of dataframe df, use:

len(df.hID.unique())

回答 5

您可以通过使用len函数来使用唯一属性

len(df [‘hID’]。unique())5

you can use unique property by using len function

len(df[‘hID’].unique()) 5


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。