[英]Modify location for for to_csv function loop in python
我正在使用 jupyter notebook 来编写我的代码。 最后,我想根据 REGION 列中的值生成几个 CSV。 我知道该怎么做。 但我不确定如何将它们全部放在一个特定的文件夹中(以下代码会自动将所有新的 CSV 放入 jupyper 笔记本位置。我正在尝试将其放入桌面的文件夹中)。
提前致谢
file_name = 'new_{0}.csv'.format(u)
df[df['REGION'] == u].to_csv(file_name, sep=',') ```
你可以这样做
import os
import pandas as pd
path = "" # Path to the folder you wish to keep your file under
for region in df['Region'].unique():
file_name = os.path.join(path,f'new_{region}.csv')
df[df['REGION'] == region].to_csv(file_name, sep=',',index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.