Guest User

Untitled

a guest
Oct 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. "vendor a::ProductA"
  2. "vendor b::ProductA
  3. "vendor a::Productb"
  4.  
  5. "vendor a"
  6. "vendor b"
  7. "vendor a"
  8.  
  9. >>> df = pd.DataFrame({'text': ["vendor a::ProductA", "vendor b::ProductA", "vendor a::Productb"]})
  10. >>> df
  11. text
  12. 0 vendor a::ProductA
  13. 1 vendor b::ProductA
  14. 2 vendor a::Productb
  15. >>> df['text_new'] = df['text'].str.split('::').str[0]
  16. >>> df
  17. text text_new
  18. 0 vendor a::ProductA vendor a
  19. 1 vendor b::ProductA vendor b
  20. 2 vendor a::Productb vendor a
  21.  
  22. >>> df['text_new1'] = [x.split('::')[0] for x in df['text']]
  23. >>> df
  24. text text_new text_new1
  25. 0 vendor a::ProductA vendor a vendor a
  26. 1 vendor b::ProductA vendor b vendor b
  27. 2 vendor a::Productb vendor a vendor a
  28.  
  29. # Select the pandas.Series object you want
  30. >>> df['text']
  31. 0 vendor a::ProductA
  32. 1 vendor b::ProductA
  33. 2 vendor a::Productb
  34. Name: text, dtype: object
  35.  
  36. # using pandas.Series.str allows us to implement "normal" string methods
  37. # (like split) on a Series
  38. >>> df['text'].str
  39. <pandas.core.strings.StringMethods object at 0x110af4e48>
  40.  
  41. # Now we can use the split method to split on our '::' string. You'll see that
  42. # a Series of lists is returned (just like what you'd see outside of pandas)
  43. >>> df['text'].str.split('::')
  44. 0 [vendor a, ProductA]
  45. 1 [vendor b, ProductA]
  46. 2 [vendor a, Productb]
  47. Name: text, dtype: object
  48.  
  49. # using the pandas.Series.str method, again, we will be able to index through
  50. # the lists returned in the previous step
  51. >>> df['text'].str.split('::').str
  52. <pandas.core.strings.StringMethods object at 0x110b254a8>
  53.  
  54. # now we can grab the first item in each list above for our desired output
  55. >>> df['text'].str.split('::').str[0]
  56. 0 vendor a
  57. 1 vendor b
  58. 2 vendor a
  59. Name: text, dtype: object
  60.  
  61. def do_it(str):
  62. integer=0
  63. while integer<len(str):
  64. if str[integer]==':' :
  65. if str[integer+1]==':' :
  66. str=str.split(':')[0]
  67. break;
  68. integer=integer+1
  69. return (str)
Add Comment
Please, Sign In to add comment