Advertisement
elena1234

range , apply and lambda in Python

Mar 5th, 2023 (edited)
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. def range(series):
  2.     return series.max() - series.min()
  3.  
  4. df.apply(range, axis = 0)
  5. # or
  6. df.apply(lambda series: series.max() - series.min(), axis = 0)
  7. '''
  8. Mon 20
  9. Thu 13
  10. Wed 25
  11. '''
  12.  
  13. df.apply(range, axis = 1)
  14. # or
  15. df.apply(lambda series: series.max() - series.min(), axis = 1)
  16. '''
  17. Steven  18
  18. Elan  30
  19. Ivelin  13
  20. '''
  21.  
  22.  
  23. ########################################################
  24. summer.Athlete.apply(lambda x: x[0]) # working with all elements in Athlete column
  25. summer.iloc[:, 1:3].applymap(lambda x: x[0]) # working with all elements in first and second column
  26.  
  27.  
  28. ########################################################
  29. # Calculate for all numerical columns the range between highest and lowest value!
  30. cars.iloc[:, 0:-2].apply(lambda x: x.max() - x.min(), axis = 0)
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement