SHARE
TWEET

pandas - convert string into many columns

furas Aug 6th, 2018 (edited) 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data = [
  2.     'TTMNPMGN=31.28954;NLOW=307.97567;TTMPRCFPS=27.16872',
  3.     'NLOW=307.97567;TTMPRCFPS=27.16872;TTMGROSMGN=49.10444',
  4. ]    
  5.  
  6. import pandas as pd
  7.  
  8. print('\n--- original data ---\n')
  9.  
  10. df1 = pd.DataFrame(data)
  11. print(df1.head())
  12.  
  13. print('\n--- splitted to columns ---\n')
  14.  
  15. df2 = pd.DataFrame()
  16.  
  17. for index, row in df1.iterrows():
  18.     d = dict(item.split('=') for item in row[0].split(';'))
  19.     df2 = df2.append(d, ignore_index=True)
  20.  
  21. print(df2.head())
  22.  
  23. print('\n--- filled missing values ---\n')
  24.    
  25. #df2.fillna('0', inplace=True)    
  26. df2['TTMNPMGN'].fillna('999', inplace=True)    
  27. df2['TTMGROSMGN'].fillna('-999', inplace=True)    
  28. print(df2.head())
  29.  
  30. # save using "columns" to keep order of columns in file
  31. df2.to_csv('output.csv', index=False, columns=['TTMNPMGN', 'NLOW', 'TTMPRCFPS', 'TTMGROSMGN'])
  32.  
  33. '''
  34. --- original data ---
  35.  
  36.                                                   0
  37. 0  TTMNPMGN=31.28954;NLOW=307.97567;TTMPRCFPS=27....
  38. 1  NLOW=307.97567;TTMPRCFPS=27.16872;TTMGROSMGN=4...
  39.  
  40. --- splited to columns ---
  41.  
  42.        NLOW  TTMNPMGN TTMPRCFPS TTMGROSMGN
  43. 0  307.97567  31.28954  27.16872        NaN
  44. 1  307.97567       NaN  27.16872   49.10444
  45.  
  46. --- filled missing values ---
  47.  
  48.        NLOW  TTMNPMGN TTMPRCFPS TTMGROSMGN
  49. 0  307.97567  31.28954  27.16872       -999
  50. 1  307.97567       999  27.16872   49.10444
  51. '''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top