0

I read csv file with table df = pd.read_csv('Discount.csv')

Product         Price           Discount
product name    849.00 EGP      15
product name    39.00 EGP       43
product name    1,889.00 EGP    46
product name    220.00 EGP      11
product name    7,777.00 EGP    13
product name    279.00 EGP      44

i need to delete the full row if column "Discount" contains a cell less than 40

Ahmed Mohsen
  • 1
  • 1
  • 6

1 Answers1

0

How about keeping the rows with "Discount" greater than or equal to 40?

df =  # Your data.
print(df)

        Product        Price  Discount
0  product name   849.00 EGP        15
1  product name    39.00 EGP        43
2  product name  1889.00 EGP        46
3  product name   220.00 EGP        11
4  product name  7777.00 EGP        13
5  product name   279.00 EGP        44


# You can assign it to a new dataframe as well.
df_dropped = df[~(df['Discount'] < 40)]
print(df_dropped)

        Product        Price  Discount
1  product name    39.00 EGP        43
2  product name  1889.00 EGP        46
5  product name   279.00 EGP        44

It may be a good idea to reset the index numbers, which can be achieved using reset_index() function.

# Beware that this will modify the dataframe inplace,
# meaning that, it does not return a new dataframe.
df_dropped.reset_index(drop=True, inplace=True)
Ibrahim Berber
  • 842
  • 2
  • 16
  • it's not working df = pd.read_csv('Discount.csv') df_dropped = df[~(df['Price'] < 40)] print(df_dropped) Error line 64, in df_dropped = df[~(df['dis'] < 40)] Error File "pandas\_libs\ops.pyx", line 103, in pandas._libs.ops.scalar_compare TypeError: '<' not supported between instances of 'str' and 'int' – Ahmed Mohsen Jun 22 '21 at 08:20
  • Can you check the `dtype` of _Price_ column? – Ibrahim Berber Jun 22 '21 at 08:33
  • 1
    Just to be clear, i want to make changes on "Discount" column not "price" And yes i checked data type they are all numbers, no strings – Ahmed Mohsen Jun 22 '21 at 10:29
  • Thanks for noticing the typo. Now, it is filtering based on "Discount" column. – Ibrahim Berber Jun 23 '21 at 15:42