jeudi 23 septembre 2021

Python - Remove row if item is above certain value and replace if between other values

I'm working in a pandas dataframe trying to clean up some data and I want to assign multiple rules to a certain column. If the column value is greater than 500 I want to drop the column. If the column value is between 101 and 500 I want to replace the value with 100. When the column is less than 101 return the column value.

As you can see from the image I want to drop the Item B row, and adjust the item C row

I'm able to do it in 2 lines of code, but I was curious if there's a cleaner more efficient way to do this. I tried with an If/Elif/Else, but I couldn't get it to run or a lambda function, but again I couldn't get it to run.

# This drops all rows that are greater than 500
df.drop(df[df.Percent > 500].index, inplace = True)

# This sets the upper limit on all values at 100
df['Percent'] = df['Percent'].clip(upper = 100)

Aucun commentaire:

Enregistrer un commentaire