Guest User

Python script 3

a guest
Aug 29th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 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. 30,0.3
  27. 10,0.9
  28.  
  29. Command:
  30. python script.py
  31.  
  32. Output:
  33. key 10.0 finished
  34. Traceback (most recent call last):
  35. File "script.py", line 13, in <module>
  36. result = np.vstack((result, col))
  37. File "/home/user/.local/lib/python2.7/site-packages/numpy/core/shape_base.py", line 234, in vstack
  38. return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
  39. ValueError: all the input array dimensions except for the concatenation axis must match exactly
Advertisement
Add Comment
Please, Sign In to add comment