【问题标题】:Update values in a pandas dataframe based on multiple conditions [duplicate]根据多个条件更新熊猫数据框中的值[重复]
【发布时间】:2020-01-03 00:58:20
【问题描述】:

您好,尝试根据多个条件更新数据框中的值。如果有低于 30 美元的 Gas/Fuel 类别,我想将类别更改为食品。

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'

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    用途:

    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')
    

    【讨论】:

      猜你喜欢
      • 2019-07-31
      • 2023-02-21
      • 1970-01-01
      • 2019-09-15
      • 2017-10-04
      • 2019-07-25
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多