根据多种条件更新熊猫数据框中的值

时间:2020-01-03 00:58:20

标签: python pandas

您好,尝试根据多种条件更新数据框中的值。如果有汽油/燃料价格低于30美元的类别,我想将类别更改为食物。

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'

1 个答案:

答案 0 :(得分:0)

使用:

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

df['Category'] = df['Category'].mask(c,'Food')
#df['Category'] = df['Category'].where(~c,'Food')