Home > Blockchain >  Exporting each row in Pandas Dataframe to Separate CSVs
Exporting each row in Pandas Dataframe to Separate CSVs

Time:05-01

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')

CodePudding user response:

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')

CodePudding user response:

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')

CodePudding user response:

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('pagenumber\tnew_tags\n')
        wfile.write(f'{pagenumber}\t{new_tags}\n')
  • Related