将 Pandas 数据框中的每一行导出到单独的 CSV
Exporting each row in Pandas Dataframe to Separate CSVs
我有这样一个数据框:
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
我怎样才能从 Pandas DF 中取出每一行并将其分别存储在新的 CSV 中。
到目前为止我有这个,但它似乎没有用。我正在使用 f'strings 根据循环数据关联的 PageNumber 命名 csv。
for PageNumber, data in central_edi_posts_grouped.iterrows():
data.to_csv(rf'Grid_tags/grid_{PageNumber}.csv')
不确定预期的输出是什么,但如果您想保留行,请按帧切片:
# 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')
你的方案其实很好。你只需要做一些调整。
当您循环 (df.iterrows()) 时,您将不会获得页码和数据。您将获得行和页数据的索引。另外你在 f'' 之前有一个 r,应该删除它。
检查一下:
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')
这种代码变体创建了一个子目录 data_test
,并且给定数据帧的每一行都写入一个单独的 csv
-文件 header。希望这段代码能帮助您理解 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')
我有这样一个数据框:
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
我怎样才能从 Pandas DF 中取出每一行并将其分别存储在新的 CSV 中。
到目前为止我有这个,但它似乎没有用。我正在使用 f'strings 根据循环数据关联的 PageNumber 命名 csv。
for PageNumber, data in central_edi_posts_grouped.iterrows():
data.to_csv(rf'Grid_tags/grid_{PageNumber}.csv')
不确定预期的输出是什么,但如果您想保留行,请按帧切片:
# 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')
你的方案其实很好。你只需要做一些调整。
当您循环 (df.iterrows()) 时,您将不会获得页码和数据。您将获得行和页数据的索引。另外你在 f'' 之前有一个 r,应该删除它。
检查一下:
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')
这种代码变体创建了一个子目录 data_test
,并且给定数据帧的每一行都写入一个单独的 csv
-文件 header。希望这段代码能帮助您理解 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')