Home > Software design >  Pandas: New dataframe grouping by 2 text columns
Pandas: New dataframe grouping by 2 text columns

Time:01-24

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?

CodePudding user response:

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

CodePudding user response:

Select the columns you need and drop duplicates

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