Created
April 19, 2017 15:28
-
-
Save semyont/2c31968225e67c44833b69651d31a15e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert wide format csv to long format csv | |
# Time Temp1 Temp2 Temp3 Temp4 Temp5 | |
# 00 21 32 33 21 23 | |
# 10 34 23 12 08 23 | |
# 20 12 54 33 54 55 | |
with open("in.csv") as f,open("out.csv","w") as out: | |
headers = next(f).split()[1:] # keep headers/Time Temp1 Temp2 Temp3 Temp4 Temp5 | |
for row in f: | |
row = row.split() | |
time = row[0] | |
data = zip(headers, row[1:]) # match correct temp to row item | |
for a, b in data: | |
out.write("{} {} {}\n".format(time,a.lower(),b)) | |
print("{} {} {}".format(time,a.lower(),b)) | |
# 00 temp1 21 | |
# 00 temp2 32 | |
# 00 temp3 33 | |
# 00 temp4 21 | |
# 00 temp5 23 | |
# 10 temp1 34 | |
# 10 temp2 23 | |
# 10 temp3 12 | |
# 10 temp4 08 | |
# 10 temp5 23 | |
# 20 temp1 12 | |
# 20 temp2 54 | |
# 20 temp3 33 | |
# 20 temp4 54 | |
# 20 temp5 55 | |
# Convert wide format csv to long format csv | |
# Time Temp1 Temp2 Temp3 Temp4 Temp5 | |
# 00 21 32 33 21 23 | |
# 10 34 23 12 08 23 | |
# 20 12 54 33 54 55 | |
# Time Temp1 Temp2 Temp3 Temp4 Temp5 | |
# 00 21 32 33 21 23 | |
# 10 34 23 12 08 23 | |
# 20 12 54 33 54 55 | |
with open("in.csv") as f,open("out.csv","w") as out: | |
headers = next(f).rstrip('\r\n').split(',')[1:] # keep headers/Time Temp1 Temp2 Temp3 Temp4 Temp5 | |
for row in f: | |
if ('DateTime' in row): | |
headers = row.rstrip('\r\n').split(',')[1:] | |
continue | |
row = row.rstrip('\r\n').split(',') | |
time = datetime.datetime.strptime(row[0],'%d/%m/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S') | |
data = zip(headers, row[1:]) # match correct temp to row item | |
for a, b in data: | |
out.write("{},{},{}\n".format(a, time, b)) | |
###################### | |
###################### | |
# group, id, name | |
# A, 12345, "eeny" | |
# A, 23456, "meeny" | |
# A, 34567, "miney mo" | |
# B, 99999, "foo" | |
# B, 88888, "bar" | |
# B, 77777, "foobar" | |
df['group_num'] = df.groupby('group')['id'].transform(lambda x: range(1, len(x)+1)) | |
df = df.pivot(index='group', columns='group_num') | |
# id name | |
# group_num 1 2 3 1 2 3 | |
# group | |
# A 12345 23456 34567 eeny meeny miney mo | |
# B 99999 88888 77777 foo bar foobar | |
df.columns = [''.join([lvl1, str(lvl2)]) for lvl1, lvl2 in df.columns] | |
df | |
# id1 id2 id3 name1 name2 name3 | |
# group | |
# A 12345 23456 34567 eeny meeny miney mo | |
# B 99999 88888 77777 foo bar foobar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment