简体   繁体   English

根据 python 中的列中的值将 dataframe 导出到多个 csv 文件

[英]Exporting a dataframe to several csv files based on value in a column in python

I have the following code for exporting each "id" to a separate csv file.我有以下代码用于将每个“id”导出到单独的 csv 文件。 Each csv file should be named as an id.每个 csv 文件都应该命名为一个 id。 Here in my case, we should have three different csv files named 23, 24, and 25. I have the code in below but it gives me an error.在我的例子中,我们应该有三个不同的 csv 文件,分别命名为 23、24 和 25。我有下面的代码,但它给了我一个错误。 Thanks.谢谢。

import pandas as pd

path = "users/AR/csv files"

for (id), id in df.groupby(['id']):
     group.to_csv(f'{id}.csv', directory=path, index=False)
NameError: name 'group' is not defined
id ID date日期 count数数
23 23 2/2/2016 2/2/2016 24 24
24 24 2/4/2016 2/4/2016 56 56
25 25 2/3/2016 2/3/2016 135 135
23 23 3/4/2016 2016 年 3 月 4 日 46 46
24 24 3/8/2016 2016 年 3 月 8 日 176 176
25 25 3/9/2016 2016 年 3 月 9 日 23 23
23 23 3/16/2016 2016 年 3 月 16 日 98 98
24 24 3/13/2016 2016 年 3 月 13 日 114 114
25 25 3/17/2016 2016 年 3 月 17 日 43 43

I am expecting three separated csv files in the directory.我期望目录中有三个单独的 csv 文件。

Just change:只是改变:

for (id), id in df.groupby(['id']):

with:和:

for id, group in df.groupby(['id']):

When iterating groupby object, you get separate grouping value (in id , in your case), from particular group sub-dataframe (here: in group ).当迭代 groupby object 时,您会从特定的组子数据帧(此处:在group中)获得单独的分组值(在您的情况下为id )。 id is then used to name a csv file, while group df is exported to csv.然后用id命名一个csv的文件,而group df导出到csv。

Furthermore, there is no directory parameter in to_csv method.此外, to_csv方法中没有directory参数。 The easiest method is to include path in the filename string, like f'users/AR/csv files/{id}.csv' .最简单的方法是在文件名字符串中包含路径,例如f'users/AR/csv files/{id}.csv' If you need OS-agnostic solution, you can use os.path method.如果您需要与操作系统无关的解决方案,您可以使用os.path方法。

This can be done with list comps:这可以通过 list comps 来完成:

dfs = [x.reset_index(drop=True) for _, x in df.groupby("id")]
[x.to_csv(f"users/AR/csv files/{x['id'][0]}.csv", index=False) for x in dfs]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有一种方法可以根据数据框的列中的不同名称写入和保存多个csv或excel文件? - Is there a way to write & save several csv or excel files based on different names in a column of a dataframe? 根据 pandas dataframe 中的列值导出许多 excel 文件,同时保持格式? - Exporting many excel files based on column value in pandas dataframe while maintaining formatting? 需要帮助根据列中的值将 dataframe 导出到多个 CSV - Python - Need help Exporting a dataframe into multiple CSVs based on a the values in a column - Python 使用数据框操作后,根据列值创建多个 csv/excel 文件 - create multiple csv/excel files based on column value after operation with dataframe 将 dataframe 导出到 csv 不显示第一列 - Exporting dataframe to csv not showing first column Python 将字典转换为 dataframe 并将其导出到 csv 文件 - Python converting dictionary to dataframe and exporting it to a csv file 根据列值过滤Python中的熊猫dataframe - Filtering panda dataframe in Python based on column value 基于列值的Python Dask数据框分离 - Python Dask dataframe separation based on column value Python:Pandas - 根据列值分隔Dataframe - Python: Pandas - Separate a Dataframe based on a column value Python - Pandas 2 dataframe 基于列值计算 - Python - Pandas 2 dataframe calculation based on column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM