0

I have a dataframe type are:

FiscalYear      int64
GL             object
GLID            int64
GL_Debit      float64
GL_Credit     float64
month          object
diffDebit     float64
diffCredit    float64
diffval       float64

I try to modify a float column (diffvalue) to multiply it on a condition My code is:

df['diffval'] =  df.apply(lambda x: x['diffval']*-1 if x['GL'].str[0] in ['4','0'] else x['diffval'])

Im trying to do this but I have an error:

KeyError: ('GL', 'occurred at index FiscalYear')

dataframe sample :

[![enter image description here][1]][1] thanks

FiscalYear  GL  GLID    GL_Debit    GL_Credit   month   diffDebit   diffCredit  diffval
2020    1001 Banque:RBC 06621-1006014 (Salle Privée)    70  19386,69    0   2020-01-01  19386,69    0   19386,69
2020    1001 Banque:RBC 06621-1006014 (Salle Privée)    70  0   5074,84 2020-02-01  -19386,69   5074,84 -24461,53
2020    1002 Banque:Desjardins 0282527 (Traiteurs)  71  2758,45 0   2020-01-01  2758,45 0   2758,45
2020    1002 Banque:Desjardins 0282527 (Traiteurs)  71  2765,64 0   2020-02-01  7,19    0   7,19
2020    1003 Banque:Desjardins 0282857 (ED) 54  36725,91    0   2020-01-01  36725,91    0   36725,91
2020    1003 Banque:Desjardins 0282857 (ED) 54  117149,35   0   2020-02-01  80423,44    0   80423,44
2020    1004 Banque:RBC 05261-1020403   231 30282,34    0   2020-01-01  30282,34    0   30282,34
2020    1004 Banque:RBC 05261-1020403   231 2277,34 0   2020-02-01  -28005  0   -28005
2020    1061 Dépôt Stripe à recevoir:Stripe - Salle Privée  219 7208,32 0   2020-01-01  7208,32 0   7208,32
2020    1061 Dépôt Stripe à recevoir:Stripe - Salle Privée  219 6015,4  0   2020-02-01  -1192,92    0   -1192,92
2020    1062 Dépôt Stripe à recevoir:Stripe - Traiteurs.Co  220 1643,28 0   2020-01-01  1643,28 0   1643,28
2020    1062 Dépôt Stripe à recevoir:Stripe - Traiteurs.Co  220 2678,28 0   2020-02-01  1035    0   1035
2020    1099 Fonds non déposés  8   1200    0   2020-01-01  1200    0   1200
2020    1099 Fonds non déposés  8   0   0   2020-02-01  -1200   0   -1200
2020    1100 Compte clients (C/C)   74  65665,57    0   2020-01-01  65665,57    0   65665,57
2020    1100 Compte clients (C/C)   74  94235,09    0   2020-02-01  28569,52    0   28569,52
2020    1300 Actif du stock 68  0   0   2020-01-01  0   0   0
2020    1300 Actif du stock 68  0   0   2020-02-01  0   0   0
10
  • Whatever x is, it doesn't have a 'GL' key. I wonder how we are supposed to find the problem when we don't know the contents of the DataFrame - at least a minimal example of it - minimal reproducible example Commented Mar 1, 2020 at 15:27
  • can you provide some data sample and the expected output for that Commented Mar 1, 2020 at 15:27
  • Which axis are you applying the function to?? pandas.pydata.org/docs/reference/api/… Commented Mar 1, 2020 at 15:31
  • Sorry Guys, Answers: Commented Mar 1, 2020 at 15:34
  • Dataframe has 9 columns: FiscalYear, GL, GLID, GL_Debit, GL_Credit, month, diffDebit, DiffCredit, diffVal Commented Mar 1, 2020 at 15:36

2 Answers 2

1

You are indexing the dataframe along columns. So when using lambda, specify the axis as 1:

df['diffval'] = df.apply(lambda x: x['diffval'] * -1 if x['GL'].str[0] in ['4', '0'] else x['diffval'], axis=1)

Just noticed your comment,

You do not need to specify .str as it is already a string.

df['diffval'] = df.apply(lambda x: x['diffval'] * -1 if x['GL'][0] in ['4', '0'] else x['diffval'], axis=1)

If it is required to convert to string, you may use,

df['diffval'] = df.apply(lambda x: x['diffval'] * -1 if str(x['GL'])[0] in ['4', '0'] else x['diffval'], axis=1)

Does this work, or am I missing something?

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers! if you find it suitable, you may accept the answer to close the topic.
1

For your problem, you don't need to use the apply command (which is slow). You can solve this using loc.
Select rows where GL starts with '4' or '0' and then multiply to -1 the diffval column

mask = df['GL'].str[0].isin(['4','0'])

df.loc[mask, 'diffval'] = df.loc[mask, 'diffval'] * -1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.