0

I'm trying to drop specific rows of my dataset, and I always have the same error. I have several candidates and I just want to work with two. I tried with "not in"condition, but when I need to visualize it used all the candidates. I used the same formula to delete columns and it worked! Any ideas? Thanks

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

I also tried to by adding axis=1 at the end.

dataset: https://projects.fivethirtyeight.com/polls-page/president_polls.csv

1 Answers1

0

Assuming that this is your list of names,

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 is your dataframe, and the column that contains these names is called Candidates

Use this:

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

Ji Wei
  • 840
  • 9
  • 19