Advertisement
elena1234

create a column based on other column, apply , lambda in Python

Mar 2nd, 2023 (edited)
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | Source Code | 0 0
  1. '''
  2. We need to calculate the sales tax and total amount owed for every order in this sheet.
  3.  
  4.    If location is Sun Valley, apply a sales tax of 8%
  5.    If location is Mammoth, apply a sales tax of 7.75%.
  6.    If location is Stowe, apply a sales tax of 6%
  7. '''
  8.  
  9. df['Tax'] = df.apply(lambda row: 0.08 if row['Location'] == 'Sun Valley'
  10.                      else (0.0775 if row['Location'] == 'Mammoth'
  11.                            else (0.06 if row['Location'] == 'Stowe'
  12.                                  else row['Tax'])), axis=1)
  13.  
  14. df.loc[:, ['Tax', 'Location']]
  15.  
  16. ###########################################################
  17. '''
  18. I have a Subtotal column and a Tax column in the dataframe and I want based on these two columns to write values ​​to the third column Total.
  19. '''
  20. df['Total'] = df.apply(lambda row: row['Subtotal'] + (row['Tax'] * row['Subtotal']), axis=1)
  21. df['Total'] = df['Subtotal'] + (df['Tax'] * df['Subtotal])
  22.  
  23. titanic['fare'] = titanic['fare'] * 10
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement