I have the following set of rows in a pandas DF:
idx | col1 | col2 | col3 | col4 |
---|---|---|---|---|
0 | A | B | C | D |
1 | E | F | G | H |
1 | I | J | K | L |
2 | M | M | O | P |
2 | Q | R | S | T |
I want to convert each set of indexed rows to CSV and print to file.
So that I end up with a file with one row for idx 0, two rows for idx 1, and two rows for idx 2.
Like so:
file1
col1,col2,col3,col4
A,B,C,D
file2
col1,col2,col3,col4
E,F,G,H
I,J,K,L
file3
col1,col2,col3,col4
M,N,O,P
Q,R,S,T
I have this code, but it only gives me the first row of each index set:
for i, dfr in Template.TEMPLATE_DF.iterrows():
fpath = path + '\\' + dfr['tmpl.title'].lower().replace(' ', '_') + '_' + str(dfr['tmpl.id']) + '.csv'
dfr=pd.DataFrame(data=dfr).transpose()
dfr.to_csv(fpath, sep=',', encoding='utf-8', na_rep='NULL', index=False)
What am I missing here?