Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. "New york", "Atlanta", "Mumbai"
  2. "Beijing", "Paris", "Budapest"
  3. "Brussels", "Oslo", "Singapore"
  4.  
  5. New york Atlanta
  6. Beijing Paris Budapest
  7. Brussels Oslo Singapore
  8.  
  9. >>> df
  10. 0 1 2
  11. 0 New york Atlanta Mumbai
  12. 1 Beijing Paris Budapest
  13. 2 Brussels Oslo Singapore
  14.  
  15. >>> df.apply(" ".join, axis=1)
  16. 0 New york Atlanta Mumbai
  17. 1 Beijing Paris Budapest
  18. 2 Brussels Oslo Singapore
  19. dtype: object
  20.  
  21. >>> df.apply(" ".join, axis=0)
  22. 0 New york Beijing Brussels
  23. 1 Atlanta Paris Oslo
  24. 2 Mumbai Budapest Singapore
  25. dtype: object
  26.  
  27. df[0].str.cat(df.ix[:, 1:].T.values, sep=' ')
  28.  
  29. 0 New york Atlanta Mumbai
  30. 1 Beijing Paris Budapest
  31. 2 Brussels Oslo Singapore
  32. Name: 0, dtype: object
  33.  
  34. %timeit df.apply(" ".join, axis=1)
  35. 10 loops, best of 3: 112 ms per loop
  36.  
  37. %timeit df[0].str.cat(df.ix[:, 1:].T.values, sep=' ')
  38. 100 loops, best of 3: 4.48 ms per loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement