简体   繁体   English

如何根据 python 中的另一个列值从列中找到最常见的值?

[英]How can I find the most common value from a column based on another Columns value in python?

I have a dataframe (DF) that has the following columns: UserID, Country, Arrival_Year, Airport_Code.我有一个 dataframe (DF),其中包含以下列:UserID、Country、Arrival_Year、Airport_Code。 Each country is listed several times based on UserID.每个国家根据用户 ID 被多次列出。

I want to know the most common arrival year for each country listed in the data frame.我想知道数据框中列出的每个国家/地区最常见的到达年份。 How can I calculate that?我该如何计算?

Tried using value counts but not getting the right answer.尝试使用值计数但没有得到正确的答案。

Try using pandas groupby with mode:尝试使用 pandas groupby 模式:

df.groupby('Country')['ArrivalYear'].apply(lambda x: x.mode()[0])

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

相关问题 如何将一列添加到基于另一个列值的数据框中? - How can I add a column to a dataframe that is based on another columns value? 如何根据 python 中另一列的条件查找两个日期之间特定列的最大值 - How do I Find max value of a particular column between 2 dates based on a condition from another column in python 使用python在CSV文件的列中查找最常用的值 - Using python to find the most common value(s) in the column of CSV file Numpy 从另一列中找到每个值最常见的项目 - Numpy finds most common item per value from another column 根据python另一列中的公共值在数据框的一列中划分2个值 - Dividing 2 values in a column of dataframe based on common value in another column in python 如何根据另一列值Python总结一列 - How to sum up a column based on another columns value Python 当 Python 中的一列值不明确时,如何根据其他两列获取值? - How can I get a value based on two other columns when one column value is ambigous in Python? 基于pandas中的公共列值将多列合并在一起-python - Merge multiple columns together based on a common column value in pandas - python Python在数组中找到最常见的值 - Python find most common value in array 如何从python中的整列字符串中找到最常见的单词 - how to find most common word from the entire column of string in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM