Guest User

Untitled

a guest
Sep 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. sep = ' - '
  4. df1 = pd.DataFrame([[1,"apple",1],[1,"banana",3],[1,"pear",2],[2,"lemon",3],[2,"orange",1],[2,"apple",2]])
  5. df1.columns = ["id","product","seq"]
  6. df1 = df1.sort_values(by=["id","seq"])
  7. pd.DataFrame(df1.groupby('id',as_index=False)['product'].apply(lambda x: sep.join(x)))
  8.  
  9. df2 = df1.groupby('id',as_index=False, sort=False)['product'].apply(sep.join)
  10. print (df2)
  11. 0 apple - pear - banana
  12. 1 orange - apple - lemon
  13. dtype: object
Add Comment
Please, Sign In to add comment