Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
62 views
in Technique[技术] by (71.8m points)

python - 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

question from:https://stackoverflow.com/questions/65660874/how-do-i-delete-a-list-of-rows-from-a-dataframe-in-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do isin

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...