【问题标题】:Erorr with drop specific rows with Pandas使用 Pandas 删除特定行时出错
【发布时间】:2020-09-30 03:08:09
【问题描述】:

我正在尝试删除数据集的特定行,但我总是遇到同样的错误。我有几个候选人,我只想和两个人一起工作。我尝试了“不在”状态,但是当我需要将其可视化时,它使用了所有候选人。 我使用相同的公式删除列并且它有效! 有任何想法吗? 谢谢

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.com/polls-page/president_polls.csv

【问题讨论】:

  • 欢迎堆栈溢出!请提供您的输入数据框和预期输出的示例,以及您看到的任何错误的完整错误回溯,以制作minimal reproducible example
  • 请添加具有预期输出的示例数据集
  • 我刚刚添加了数据集的链接。

标签: python pandas delete-row drop


【解决方案1】:

假设这是您的姓名列表,

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)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2018-05-09
    • 2019-04-23
    • 1970-01-01
    • 2021-10-02
    • 2014-10-29
    相关资源
    最近更新 更多