Exporting each row in Pandas Dataframe to Separate CSVs

I have a dataframe as such:

df = pd.DataFrame({'PageNumber': [175, 162, 576], 'new_tags': [['flower architecture people'], ['hair red bobbles'], ['sweets chocolate shop']})

<OUT>
PageNumber   new_tags
   175       flower architecture people
   162       hair red bobbles
   576       sweets chocolate shop

How am I able to take each row from the Pandas DF and store it in a new CSV separately.

So far I have this, but it doesn’t seem to have worked. I am using f’strings to name the csv based on the PageNumber in which the data looped was associated with.

for PageNumber, data in central_edi_posts_grouped.iterrows():
       data.to_csv(rf'Grid_tags/grid_{PageNumber}.csv')

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your solution is actually good. You just need to make few adjustments.

When you loop on (df.iterrows()) you will not get the PageNumber and data. You will get the index of the row and page data. Also you had an r before the f”, which should be removed.

Chack this out:

import pandas as pd
df = pd.DataFrame({'PageNumber': [175, 162, 576], 'new_tags': [['flower architecture people'], ['hair red bobbles'], ['sweets chocolate shop']]})

for index, data in df.iterrows():
    data.to_csv(f'Grid_tags/grid_{data["PageNumber"]}.csv')

Method 2

Not sure what is the expected output, but if you want to keep rows, slice as frames:

# if needed to have PageNumber as index:
central_edi_posts_grouped = central_edi_posts_grouped.set_index('PageNumber')

# then loop
for idx in central_edi_posts_grouped.index:
    central_edi_posts_grouped.loc[[idx]].to_csv(rf'Grid_tags/grid_{idx}.csv')

Method 3

This variant of code makes a subdirectory data_test and each row of given dataframe is writing to a separate csv-file with header. Hope this code will help you to understand f-strings

import pandas as pd
import os

df = pd.DataFrame({'PageNumber': [175, 162, 576], 'new_tags': [['flower architecture people'], ['hair red bobbles'], ['sweets chocolate shop']]})

print(df)

FOLDER_EXPORT = 'data_test'
os.makedirs(FOLDER_EXPORT, exist_ok=True)

for index, row in df.iterrows():
    # print( row['PageNumber'], row['new_tags'])
    pagenumber = row['PageNumber']
    new_tags = ';'.join(row['new_tags'][0].split(' '))
    # print(pagenumber, new_tags)
    
    filename_csv = f'./{FOLDER_EXPORT}/grid_{pagenumber}.csv'

    with open(filename_csv, mode='w', encoding='utf-8') as wfile:
        if wfile.tell() == 0:
            wfile.write('pagenumbertnew_tagsn')
        wfile.write(f'{pagenumber}t{new_tags}n')


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x