Zerdasht Haydari
Zerdasht Haydari

Reputation: 23

How do I delete [ a list of ] rows from a DataFrame in Pandas?

I have a list of football teams stored in a variable called big_teams. I have a dataset of all games played in a football league over many seasons. What I am trying to do is create two new dataframes, one with the big teams and one with the small teams. I am not sure where my flaw is because I am fairly new to coding and python in general.

big_teams = []
small_teams = []

    for team in different_teams:
        if n_games[team] > np.average(n_games):
            big_teams.append(team)
        elif n_games[team] < np.average(n_games):
            small_teams.append(team)

The code above works, and two new lists have been generated each with different teams.

my problem is with the code below, I had hoped that it would create two new dataframes, or more accurately dropped what i didn't want from the existing dataframe

big_df = pd.DataFrame(df)
small_df = pd.DataFrame(df)

    for team in df['HomeTeam']:
        if team in big_teams == True:
            df['HomeTeam'].drop(team)
        elif team in small_teams == True:
            df['HomeTeam'].drop(team)

Thanks in advance

Upvotes: 2

Views: 42

Answers (1)

BENY
BENY

Reputation: 323226

You can do isin

df = df[~df['HomTeam'].isin(big_teams+ small_teams)]

Upvotes: 2

Related Questions