Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- day,month,year,lat,long
- 01,04,2001,45.00,120.00
- 02,04,2003,44.00,118.00
- import csv
- with open("source","rb") as source:
- rdr= csv.reader( source )
- with open("result","wb") as result:
- wtr= csv.writer( result )
- for r in rdr:
- wtr.writerow( (r[0], r[1], r[3], r[4]) )
- in_iter= ( (r[0], r[1], r[3], r[4]) for r in rdr )
- wtr.writerows( in_iter )
- del r[2]
- wtr.writerow( r )
- import pandas as pd
- f=pd.read_csv("test.csv")
- keep_col = ['day','month','lat','long']
- new_f = f[keep_col]
- new_f.to_csv("newFile.csv", index=False)
- >>>f=pd.read_csv("test.csv")
- >>> f
- day month year lat long
- 0 1 4 2001 45 120
- 1 2 4 2003 44 118
- >>> keep_col = ['day','month','lat','long']
- >>> f[keep_col]
- day month lat long
- 0 1 4 45 120
- 1 2 4 44 118
- >>>
- import csv
- ct = 0
- cols_i_want = {'cost' : -1, 'date' : -1}
- with open("file1.csv","rb") as source:
- rdr = csv.reader( source )
- with open("result","wb") as result:
- wtr = csv.writer( result )
- for row in rdr:
- if ct == 0:
- cc = 0
- for col in row:
- for ciw in cols_i_want:
- if col == ciw:
- cols_i_want[ciw] = cc
- cc += 1
- wtr.writerow( (row[cols_i_want['cost']], row[cols_i_want['date']]) )
- ct += 1
- import csv
- file_name = 'C:Tempmy_file.csv'
- output_file = 'C:Tempnew_file.csv'
- csv_file = open(file_name, 'r')
- ## note that the index of the year column is excluded
- column_indices = [0,1,3,4]
- with open(output_file, 'w') as fh:
- reader = csv.reader(csv_file, delimiter=',')
- for row in reader:
- tmp_row = []
- for col_inx in column_indices:
- tmp_row.append(row[col_inx])
- fh.write(','.join(tmp_row))
- outFile = open( 'newFile', 'w' )
- for line in open( 'oldFile' ):
- items = line.split( ',' )
- outFile.write( ','.join( items[:2] + items[ 3: ] ) )
- outFile.close()
- del variable_name['year']
- input = [ {'day':01, 'month':04, 'year':2001, ...}, ... ]
- for E in input: del E['year']
- input = [ [01, 04, 2001, ...],
- [...],
- ...
- ]
- for E in input: del E[2]
- result= data.drop('year', 1)
- result.head(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement