简体   繁体   English

CSV 文件中最常见的值,使用 python

[英]most common value in column CSV file using python

I am beginner in python and I need to get the most common value of that column using code我是 python 的初学者,我需要使用代码获取该列的最常见值

I have next table from csv file:我有来自 csv 文件的下一张表:

elements元素 value价值
1 1 y是的
2 2 y是的
3 3 y是的
4 4 n n
5 5 n n

How I can get most common (common means that the count of y is 3, count of n is 2, so common is y) 'value'?我怎样才能得到最常见的(常见的意思是 y 的计数是 3,n 的计数是 2,所以常见的是 y)'值'?

// placeholder for 30 characters // // 30 个字符的占位符 //

df['value'].value_counts() df['value'].value_counts()

You can use count_values() and extract it from the result您可以使用count_values()并从结果中提取它

df.value.value_counts(sort=True).index[0]
# 'y'

Just be careful what should happen if there are multiple 'most common' values.如果有多个“最常见”值,请注意会发生什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM