1

I have a dataframe

id_command  command_status  stage   line_amount global_amount
61  ZeFMAA1 Pending     582.96  582.96
61  UbUjNAAV    Pending     70481.00    582.96
945 0bR8hEQAS   Pending     6400.00 12800.00

I would like to add a new column "decision" which is a boolean value (true or false) True if global_amount of a command is greater than 30% of the sum of line_amount of a command, and false if is not .

Can you help me to do this ?

Thanks

1
  • What is expected output from your sample data? Or which answer is correct? Commented May 28, 2019 at 9:11

2 Answers 2

1

Based on :

sum of line_amount of a command

Use df.groupby() to group on the id_command and use transform to get sum distributed on each row. Then multiply by 0.3 and compare by series.gt():

df['decision']=df.global_amount.gt(df.groupby('id_command').line_amount.transform('sum')*0.3)
print(df)

   id_command command_status    stage  line_amount  global_amount  decision
0          61        ZeFMAA1  Pending       582.96         582.96     False
1          61       UbUjNAAV  Pending     70481.00         582.96     False
2         945      0bR8hEQAS  Pending      6400.00       12800.00      True
Sign up to request clarification or add additional context in comments.

3 Comments

thanks anky_91, but why you and @jezrael have different results? :o
@cyrinepersonne the other result Jez calculates row by row, and this one calculates based on the sum of line amount taking id_command as a group.
I believe your answer is correct, but rather wait for OP clarification.
1

Use:

df['decision'] = df['global_amount'].gt(df['line_amount'].mul(.3))
print (df)
   id_command command_status    stage  line_amount  global_amount  decision
0          61        ZeFMAA1  Pending       582.96         582.96      True
1          61       UbUjNAAV  Pending     70481.00         582.96     False
2         945      0bR8hEQAS  Pending      6400.00       12800.00      True

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.