Search code examples
pythonpandascombinationsmultiple-columns

most occurrence of a two column combination


enter image description hereI have two columns 'Start Station' and 'End Station'

| Start Station | End Station |
|:--------------|:------------|
| A             | C           |
| A             | C           |
| B             | D           |
| C             | A           |
| A             | D           |
| C             | A           |
| C             | B           |

as you can see the most common combination is A & C I'm trying to write a code in python using pandas so that the output is "The most common combination is between A and C" I found a lot of helpful codes for this but unfortunately couldn't find a code with the output that I need. I hope I clarified my question enough and thanks in advance

I added an image because I'm new to stackoverflow and couldn't import the table example


Solution

  • idxmax will return a tuple with the most frequent combination:

    df.groupby(['Start Station', 'End Station']).value_counts().idxmax()