[英]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
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.