英文:
How do I create a reusable function to remove rows based on values in a specific column?
问题
I am using the code bellow to remove rows that contain the strings in "listToRemove" from the colours column. This works, removing the rows I want and leaving the rest. I am having trouble creating a function that can be reused to remove rows in the same way for other datasets.
df = raw_data
listToRemove = ["red", "blue"]
raw_data = raw_data[raw_data.colours.isin(list1) == False]
I have come up with this function. This does not work. How do I create a function to remove rows based on values in a specific column?
def removeRows(df, column, listToRemove):
df = df[df[column].isin(listToRemove) == False]
return df
英文:
I am using the code bellow to remove rows that contain the strings in "listToRemove" from the colours column. This works, removing the rows I want and leaving the rest. I am having trouble creating a function that can be reused to remove rows in the same way for other datasets.
df = raw_data
listToRemove = ["red", "blue"]
raw_data = raw_data[raw_data.colours.isin(list1) == False]
I have come up with this function. This does not work. How do I create a function to remove rows based on values in a specific column?
def removeRows( df, column, [list]):
listToRemove = [list]
df = df[df.column.isin(listToRemove) == False]
return df
答案1
得分: 1
一些更改:
def remove_rows(df, column, values_to_remove):
return df[~df[column].isin(values_to_remove)]
顺便尝试在 snake_case
中使用变量名称。
英文:
Some changes:
def remove_rows(df, column, values_to_remove):
return df[~df[column].isin(values_to_remove)]
BTW try to use variable names in snake_case
答案2
得分: 1
为了执行添加listToRemove
函数,您应该加载文档中的数据,类型为(csv或其他任何类型),通过pandas读取为数据框,然后应用此函数。
尝试这样做:
def removeRows(df, column, listToRemove):
df = df[df[column].isin(listToRemove)]
return df
英文:
For adding listToRemove function to be performed you should load the data present in document of type(csv or any other) read through pandas as dataframe and then apply this one.
Try this:
def removeRows(df, column, listToRemove):
df = df[df[column].isin(list_to_remove)]
return df
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论