Skip to content

Instantly share code, notes, and snippets.

@ah89
Created July 7, 2018 09:08
Show Gist options
  • Save ah89/766aeae857cc16d88ca8339c64b36167 to your computer and use it in GitHub Desktop.
Save ah89/766aeae857cc16d88ca8339c64b36167 to your computer and use it in GitHub Desktop.
flatten the the table
import csv
input_file_path = 'ehr_clean.csv'
output_file_name = 'ehr_clean_f.csv'
f = open(input_file_path)
csv_f = csv.reader(f)
rows = []
for row in csv_f:
rows.append(row)
f.close()
col_names = rows[0]
rows.pop(0)
print col_names
f2 = open(output_file_name, 'w')
f2.write('tid,attr_name,attr_val\n')
index = 1
for r in rows:
for (idx, el) in enumerate(r):
line = '{},{},{}\n'.format(str(index), col_names[idx], el)
f2.write(line)
index += 1
f2.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment