PaulSG
PaulSG

Reputation: 1

How to create a pandas dataframe from a subset of an existing pandas dataframe

I have an existing dataframe of NBA players' stats for the 2020-2021 season. I also have a list of all the names of the players that have been selected for the NBA All-Star game this year. I want to iterate over my existing dataframe and create a new dataframe that contains only stats for the players whose names are in my list of All-Star players.

For clarification, I want my new dataframe to look exactly like my existing dataframe, with the same columns, but to only contain the stats of the players whose names are listed in my All-Star players list.

Here is a picture of my existing dataframe: NBA dataframe

Here is a picture of the list of player names that I want to select from my existing dataframe: All-Star players

How can I do this? I have tried multiple different things but nothing has worked so far. Also, the names in the dataframe are strings and the names in the list are strings as well.

Upvotes: 0

Views: 83

Answers (1)

norie
norie

Reputation: 9857

Assuming the original dataframe is named df_players this should give you the stats for the players in your all stars list.

df_allstars = df_players[df_players['Player'].isin(all_star_reserves)]

Upvotes: 1

Related Questions