-3

I have a dataframe (DF) that has the following columns: UserID, Country, Arrival_Year, Airport_Code. Each country is listed several times based on UserID.

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.

  • 2
    Hi, welcome to SO! Please provide a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Add the data sample as text, not as a picture. E.g. try `df..head().to_dict()` and post in a block between triple backticks (```). Show both input *and* expected output. – ouroboros1 Dec 06 '22 at 18:17
  • Does this answer your question? [GroupBy pandas DataFrame and select most common value](https://stackoverflow.com/questions/15222754/groupby-pandas-dataframe-and-select-most-common-value) – Mahesh Vashishtha Dec 06 '22 at 18:19

2 Answers2

0

Try using pandas groupby with mode:

df.groupby('Country')['ArrivalYear'].apply(lambda x: x.mode()[0])
Nate
  • 466
  • 5
  • 23
0

This might work

df.groupby('Country')['ArrivalYear'].apply(max)
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 29 '23 at 00:17