Pandas: New dataframe grouping by 2 text columns

I have a pandas dataframe like this:

    Player          Team        League
0   Peter           Atlanta     NBA
1   Patricia        New Jersey  NWBA
2   Patricia        New York    NWBA
3   Andrew          Utah        NBA
4   John            Lakers      NBA
5   Andrew          Lakers      NBA
6   Sandie          New Jersey  WNBA

Then I need a new dataframe grouping by 'Player' and 'League', deleting the column of the teams. The result would be:

    Player          League
0   Peter           NBA
1   Patricia        NWBA
2   Andrew          NBA
3   John            NBA
4   Sandie          WNBA

I guess it's something simple but I don't get it trying:

df1 = df.groupby('Player', 'League')

Any suggestion?


Solution 1:

You don't need groupby just select the columns and then drop_duplicates:

df[['Player', 'League']].drop_duplicates()

     Player League
0     Peter    NBA
1  Patricia   NWBA
3    Andrew    NBA
4      John    NBA
6    Sandie   WNBA

Solution 2:

Select the columns you need and drop duplicates

can access columns by their index

df.iloc[:,[0,2]].drop_duplicates()

or acess columns by their names

df.loc[: ['Player', 'League']].drop_duplicates()