0

I would like to remove rows from a dataframe, whose values do not exist in a list.

I have tried this code, however it does not work as I want it to:

changelog_df: enter image description here

status_list = ['Selected for Development', 'Selected for Development', 'Finalizada', 'Backlog', 'Backlog', 'Backlog', 'En curso', 'Finalizada', 'Selected for Development']

for row in changelog_df['changelog.status.to']:
    if row != status_list:
        changelog_df.drop(changelog_df.index[changelog_df['changelog.status.to'] == row], inplace=True)

In short, I would like to delete these rows:

enter image description here

Is it possible?

Thank you in advance.

Junior P
  • 41
  • 9

2 Answers2

2

it helps if you post the data as text and not an image, to help reproduce and validate the solution.

try this out

status_list = ['Selected for Development', 'Selected for Development', 'Finalizada', 'Backlog', 'Backlog', 'Backlog', 'En curso', 'Finalizada', 'Selected for Development']


df2 = changelog_df.drop(changelog_df[changelog_df['changelog.status.to'].isin(status_list)].index)
df2

OR

df2 = changelog_df[~changelog_df['date'].isin(status_list)]
df2
Naveed
  • 11,495
  • 2
  • 14
  • 21
  • Thanks for the help, but I have tried the code and it doesn't work, it doesn't remove the rows according to the list. – Junior P Aug 08 '22 at 22:17
  • @JuniorP, I did not assign back the result, perhaps that is why. I'll update the solution – Naveed Aug 08 '22 at 23:15
0

Your condition is incorrect. You want to check if the value of a row is inside the list, not equal to the list:

if row in status_list:
TrimPeachu
  • 81
  • 5