怎么创建一个可重复使用的函数来根据特定列中的值删除行?

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月23日 22:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定