Guest User

Python script 1

a guest
Aug 29th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 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. Column A | Column B
  21. 10 | 0.1
  22. 10 | 0.2
  23. 10 | 0.3
  24. 20 | 0.4
  25. 20 | 0.5
  26. 20 | 0.6
  27.  
  28. Command:
  29. python script.py
  30.  
  31. Output:
  32. Traceback (most recent call last):
  33. File "script.py", line 4, in <module>
  34. keys = sorted(set(data[:,0]))
  35. IndexError: too many indices for array
Add Comment
Please, Sign In to add comment