Guest User

Python script 2

a guest
Aug 29th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. Python script:
  2. import numpy as np
  3.  
  4. data = np.genfromtxt('inputcsv', delimiter=',')
  5. keys = sorted(set(data[:,0]))
  6.  
  7. result = np.array([])
  8.  
  9. for k in keys:
  10. col = data[np.where(data[:,0] == k)][:,1]
  11. if not result.any():
  12. result = col
  13. else:
  14. result = np.vstack((result, col))
  15. print('key {0} finished'.format(k))
  16.  
  17. np.savetxt('final.csv', np.transpose(result), delimiter=',')
  18.  
  19. Input file:
  20. 10,0.1
  21. 10,0.2
  22. 10,0.3
  23. 20,0.4
  24. 20,0.5
  25. 20,0.6
  26.  
  27. Command:
  28. python script.py
  29.  
  30. Output:
  31. key 10.0 finished
  32. key 20.0 finished
  33.  
  34. Output file:
  35. 1.000000000000000056e-01,4.000000000000000222e-01
  36. 2.000000000000000111e-01,5.000000000000000000e-01
  37. 2.999999999999999889e-01,5.999999999999999778e-01
Add Comment
Please, Sign In to add comment