用Pandas删除特定行的错误。

问题描述 投票:0回答:1

我试图删除我的数据集的特定行,我总是有相同的错误。我有几个候选人,我只想用两个。我试过用 "不在 "的条件,但当我需要可视化它使用所有的候选人。我使用相同的公式删除列,它的工作!任何想法? 谢谢

poll = poll.drop(["Alexandria Ocasio-Cortez","Amy Klobuchar","Andrew Yang","Barack Obama","Bernard Sanders","Beto O'Rourke","Bill de Blasio","Charles E. Schumer","Elizabeth Warren","Cory A. Booker","Eric Swalwell","Howard Schultz","Hillary Rodham Clinton","John K. Delaney","Jay Robert Inslee","John Hickenlooper","Kamala D. Harris","Julián Castro","Justin Amash","Marianne Williamson","Kirsten E. Gillibrand","Megan Rapinoe","Michelle Obama","Michael F. Bennet","Michael Bloomberg","Mike Pence","Mike Gravel","Nancy Pelosi","Pete Buttigieg","Oprah Winfrey","Nimrata R. Haley","Sherrod Brown","Seth Moulton","Steve Bullock","Tulsi Gabbard","Tom Steyer","Tulsi Gabbard","Wayne Messam"])

我也试过在最后添加axis=1。

数据集。https:/projects.fivethirtyeight.compolls-pagepresident_polls.csv。

python pandas delete-row drop
1个回答
0
投票

假设这是你的名字列表。

namelist = ["Alexandria Ocasio-Cortez","Amy Klobuchar","Andrew Yang","Barack Obama","Bernard Sanders","Beto O'Rourke","Bill de Blasio","Charles E. Schumer","Elizabeth Warren","Cory A. Booker","Eric Swalwell","Howard Schultz","Hillary Rodham Clinton","John K. Delaney","Jay Robert Inslee","John Hickenlooper","Kamala D. Harris","Julián Castro","Justin Amash","Marianne Williamson","Kirsten E. Gillibrand","Megan Rapinoe","Michelle Obama","Michael F. Bennet","Michael Bloomberg","Mike Pence","Mike Gravel","Nancy Pelosi","Pete Buttigieg","Oprah Winfrey","Nimrata R. Haley","Sherrod Brown","Seth Moulton","Steve Bullock","Tulsi Gabbard","Tom Steyer","Tulsi Gabbard","Wayne Messam"]

poll 是你的数据框,而包含这些名称的列叫做 Candidates

用这个。

poll = poll[~poll['Candidates'].isin(namelist)]

© www.soinside.com 2019 - 2024. All rights reserved.