简体   繁体   English

如何找到数据帧特定列值的不同计数

[英]How to find distinct count of data frame particular column value

df: df:

Field1 Field2
1      2
3      4
5      6
1      7
3      8

Please let me know how to find distinct count of df['Field1'], Results should be 3 In SQL we use query like this. 请让我知道如何查找df ['Field1']的不同计数,结果应为3。在SQL中,我们使用这样的查询。

select count(distinct Field1) as Field1DistCount from df

Let's use nunique : 让我们使用nunique

df['Field1'].nunique()

Output: 输出:

3

I'm assuming you are using pandas? 我假设您正在使用熊猫吗?

import pandas as pd
print(df['COLUMN_NAME_HERE'].nunique())

If you want a dataframe of the unique values you could do ( and here is a good link to go along with it ): 如果您想要一个具有唯一值的数据框,则可以这样做(下面是一个很好的链接 ):

print(df['COLUMN_NAME_HERE'].unique())

And if you want the counts of those unique values using value_counts : 如果您想使用value_counts对这些唯一值进行计数:

print(df['COLUMN_NAME_HERE'].value_counts())

You can even use value_counts() to get the count of unique values for a particular value present in a column. 您甚至可以使用value_counts()获取列中存在的特定值的唯一值计数。

Example : 范例:

df['col_name'].value_counts().values #This will fetch you the count

And if you want to store the unique values in another column then you can execute the below command : 如果要将唯一值存储在另一列中,则可以执行以下命令:

df['col_name'].value_counts().index

Let me know if this helps. 让我知道是否有帮助。 Thanks 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 熊猫数据框找到具有特定列值的所有行? - pandas data frame find all rows with particular column value? 如何根据特定列中的值对数据帧的行求平均? - How to average rows of a data frame based the value in a particular column? 如何计算基于另一列的数据框列中值的频率? - How to count frequency of a value in a column of a data frame based on another column? 在 Pandas 数据框中为其查找特定的字符串和值 - Find particular string and value for it in Pandas data frame 如何使用python中的pandas查找相对于特定列的数据帧中的频率分布 - how to find frequency distribution in a data frame with respect to a particular column using pandas in python 在python中,如何从任何列中存在特定字符串的数据框中获取行(字符串值) - In python,how to get the rows from a data frame where a particular string is present in any of the column (String value) 如果列元素是一个集合,如何从 pandas 数据框列中获取每个值的计数? - How do I get count of each value from a pandas Data Frame column if the column elements is a set? 如何从数据框的最后一列中找到连续的 1 并计算多少次 - how to find consecutive 1 from last column of data frame and count how many time 从 Pandas 数据框中计算不同的单词 - Count distinct words from a Pandas Data Frame 如何在数据框列中找到不同数量的元素,其中字符串包含由分号分隔的多个元素 - How to find the distinct number of elements in data frame column, in which strings contain multiple elements separated by a semi-colon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM