0

Hello trying to update values in a dataframe based on multiple conditions. Where there is category that is Gas/Fuel less than $30 I want to change the category to food.

data.loc[60:65,['Category','Debit']]

    Category    Debit
60  Groceries   38.18
61  Gas/Fuel    7.30
62  Fast Food   9.18
63  Pharmacy    7.03
64  Gas/Fuel    3.16
65  Pharmacy    9.40

for index, row in data.iterrows():
    if row[3] == 'Gas/Fuel' and row[4] < 30:
        row[3]='Fast Food'
bryan
  • 307
  • 5
  • 19

1 Answers1

0

Use:

c = df['Category'].eq('Gas/Fuel') & df['Debit'].lt(30)
df.loc[c,'Category']='Food'

or

df['Category'] = df['Category'].mask(c,'Food')
#df['Category'] = df['Category'].where(~c,'Food')
ansev
  • 30,322
  • 5
  • 17
  • 31