Friday, 14 December 2018

How To Create Multiple Columns From Single Column using Pandas in Jupyter


  1. import pandas as pd
  2. import tensorflow as tf
  3. from pandas import DataFrame
  4. s = pd.read_excel('SaroojWork.xlsx')
  5. s = s.BelowPMandatory.str.split(',', expand=True)
  6. s.columns = ['A','B','C','D','E']
  7. s[['A','A1']] = s.A.str.split(r'[',n=1, expand =True)
  8. s['A1']= s['A1'].str.replace(']','')

  9. s[['B','B1']] = s.B.str.split(r'[',n=1, expand =True)
  10. s['B1']= s['B1'].str.replace(']','')

  11. s[['C','C1']] = s.C.str.split(r'[',n=1, expand =True)
  12. s['C1']= s['C1'].str.replace(']','')

  13. s[['D','D1']] = s.D.str.split(r'[',n=1, expand =True)
  14. s['D1']= s['D1'].str.replace(']','')

  15. s[['E','E1']] = s.E.str.split(r'[',n=1, expand =True)
  16. s['E1']= s['E1'].str.replace(']','')
  17. cols = s.columns.tolist()

  18. cols.insert(1, cols.pop(cols.index('A1')))
  19. cols.insert(3, cols.pop(cols.index('B1')))
  20. cols.insert(5, cols.pop(cols.index('C1')))
  21. cols.insert(7, cols.pop(cols.index('D1')))
  22. cols.insert(9, cols.pop(cols.index('E1')))
  23. s = s.reindex(columns= cols)

  24. writer = pd.ExcelWriter('BookSarooj.xlsx')


  25. s.to_excel(writer,'Sheet1')

  26. writer.save()




No comments:

Post a Comment

Grammar Mistakes in Technical Paper Writing

Let me share some experience in this post with you. I'm currently in the second year of my Ph.D. program. We are in the middle of paper...