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