Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. day,month,year,lat,long
  2. 01,04,2001,45.00,120.00
  3. 02,04,2003,44.00,118.00
  4.  
  5. import csv
  6. with open("source","rb") as source:
  7. rdr= csv.reader( source )
  8. with open("result","wb") as result:
  9. wtr= csv.writer( result )
  10. for r in rdr:
  11. wtr.writerow( (r[0], r[1], r[3], r[4]) )
  12.  
  13. in_iter= ( (r[0], r[1], r[3], r[4]) for r in rdr )
  14. wtr.writerows( in_iter )
  15.  
  16. del r[2]
  17. wtr.writerow( r )
  18.  
  19. import pandas as pd
  20. f=pd.read_csv("test.csv")
  21. keep_col = ['day','month','lat','long']
  22. new_f = f[keep_col]
  23. new_f.to_csv("newFile.csv", index=False)
  24.  
  25. >>>f=pd.read_csv("test.csv")
  26. >>> f
  27. day month year lat long
  28. 0 1 4 2001 45 120
  29. 1 2 4 2003 44 118
  30. >>> keep_col = ['day','month','lat','long']
  31. >>> f[keep_col]
  32. day month lat long
  33. 0 1 4 45 120
  34. 1 2 4 44 118
  35. >>>
  36.  
  37. import csv
  38. ct = 0
  39. cols_i_want = {'cost' : -1, 'date' : -1}
  40. with open("file1.csv","rb") as source:
  41. rdr = csv.reader( source )
  42. with open("result","wb") as result:
  43. wtr = csv.writer( result )
  44. for row in rdr:
  45. if ct == 0:
  46. cc = 0
  47. for col in row:
  48. for ciw in cols_i_want:
  49. if col == ciw:
  50. cols_i_want[ciw] = cc
  51. cc += 1
  52. wtr.writerow( (row[cols_i_want['cost']], row[cols_i_want['date']]) )
  53. ct += 1
  54.  
  55. import csv
  56.  
  57. file_name = 'C:Tempmy_file.csv'
  58. output_file = 'C:Tempnew_file.csv'
  59. csv_file = open(file_name, 'r')
  60. ## note that the index of the year column is excluded
  61. column_indices = [0,1,3,4]
  62. with open(output_file, 'w') as fh:
  63. reader = csv.reader(csv_file, delimiter=',')
  64. for row in reader:
  65. tmp_row = []
  66. for col_inx in column_indices:
  67. tmp_row.append(row[col_inx])
  68. fh.write(','.join(tmp_row))
  69.  
  70. outFile = open( 'newFile', 'w' )
  71. for line in open( 'oldFile' ):
  72. items = line.split( ',' )
  73. outFile.write( ','.join( items[:2] + items[ 3: ] ) )
  74. outFile.close()
  75.  
  76. del variable_name['year']
  77.  
  78. input = [ {'day':01, 'month':04, 'year':2001, ...}, ... ]
  79. for E in input: del E['year']
  80.  
  81. input = [ [01, 04, 2001, ...],
  82. [...],
  83. ...
  84. ]
  85. for E in input: del E[2]
  86.  
  87. result= data.drop('year', 1)
  88. result.head(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement