A.G.
A.G.

Reputation: 2149

Pandas Dataframe to CSV only gets the first row

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?

Upvotes: 0

Views: 658

Answers (1)

Jonathan Leon
Jonathan Leon

Reputation: 5648

this will send each grouping to the function, where it should be written to a file. check the fpath though this changes because you are no longer sending a row via iterrows, but a slice of the dataframe, so I used [0] to take the first row of x, but like I said, not sure it works because it's not test.

data='''idx tmpl.id tmpl.title  col3    col4
0   10  title one   C   D
1   20  title two   G   H
1   30  title three K   L
2   40  title four  O   P
2   50  title five  S   T'''

df = pd.read_csv(io.StringIO(data), sep=' \s+', engine='python')
def write_csv(x):
    # print(x['tmpl.title'].iat[0])
    fpath = path + '\\' + x['tmpl.title'].iat[0].lower().replace(' ', '_') + '_' + str(x['tmpl.id'].iat[0]) + '.csv' # this probably isn't correct
    x.transpose()
    x.to_csv(fpath, sep=',', encoding='utf-8', na_rep='NULL', index=False)

# df.groupby(df.index).apply(write_csv)
df.groupby('idx').apply(write_csv)

if idx is not your index, then use

df.groupby('idx').apply(write_csv)

Upvotes: 3

Related Questions