Skip to content
Advertisement

Creating a new columns with maximum count of value in multiple columns

I have a dataframe that contains multiple columns as follow:

JavaScript

I want to create a new column based on the player, competition and value of highest occurrence in Home column and Away column. Let’s say the name of a new column that I want to create is Team. I would like have a new column as follow:

enter image description here

So it supposes to assign a team for a each player for each competition. How can I do it?

Advertisement

Answer

Use custom function with GroupBy.apply with DataFrame.stack, Series.mode and first value by Series.iat:

JavaScript

Another similar idea with Series.append:

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement