Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Remove multiple rows in df that contains these values - pandas python [duplicate]

$
0
0

this is my first time posting a question on SO. I would like to learn how can I remove the entire (multiple) row of data, if one of the columns in my dataframe contained specific values?

Example: if column 'taste' exist, drop the entire row whereby country contains 'USA' and 'China'

Before:

     name   country       price1    apple  USA           2.02    apple  China         2.13    apple  South Africa  2.04    pear   China         2.85    pear   Australia     2.86    pear   USA           2.57    pear   USA           2.6

...

After:

     name   country       price1    apple  South Africa  2.02    pear   Australia     2.8

...

Here is what I written and it doesn't work:

def removeManyRows(df, column_to_check, removeValues):

// Check if col existif column_to_check not in df.columns:    print(f"The column '{column_to_check}' does not exist in the DataFrame.")    return dffor values in removeValues:    // Remove rows    df = df[df[column_to_check] != values]print(f"Success.")return df        

I am rather weak in coding so would appreciate your help/advice here! Thanks!!


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>