Home > OS >  Pandas Method chaining and adding a new column
Pandas Method chaining and adding a new column

Time:11-22

I am trying to make a new column in Pandas called "Depreciation" like below:

(omc_e.query('GL_account == "740190" and Posting_date > "2021-06-30"')
    .groupby(['Business_segment_item'], as_index=False)
    ['Amount_DKK']
    .sum()
    .assign( Depreciation = lambda x: 0 if x.Business_segment_item == "" else x.Amount_DKK)
)

But I get an error when trying to run it:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I can of course make a new dataframe "test" and run the same method and then it works:

test[['Business_segment_item', 'Amount_DKK']].apply(lambda x: 0 if x.Business_segment_item == "" else x.Amount_DKK, axis = 1)

So it must be my method chaining that is causing the trouble, but can I avoid that and still use method chaining in order to avoid a lot of temporary dataframes?

CodePudding user response:

Use numpy.where:

(omc_e.query('GL_account == "740190" and Posting_date > "2021-06-30"')
    .groupby(['Business_segment_item'], as_index=False)
    ['Amount_DKK']
    .sum()
    .assign( Depreciation = lambda x: np.where(x.Business_segment_item == "" , 0, x.Amount_DKK))
)
  • Related