[英]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 ![]() |
y![]() |
2 ![]() |
y![]() |
3 ![]() |
y![]() |
4 ![]() |
n ![]() |
5 ![]() |
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.