简体   繁体   English

Python-如何使用不同的文件名保存CSV文件?

[英]Python - How to save CSV files with different file names?

I have 5 DataFrame with columns 'day' , 'number' , 'id' , 'recordDay' , and I put all the 5 dataframes in a dictionary . 我有5个DataFrame 'day''number''id''recordDay' ,并将这5个数据都放在dictionary I would like to save 5 dataframes in 5 CSV files with file names based on 'id' and 'recordDay' . 我想将5个数据保存在5个CSV文件中,文件名基于'id''recordDay' Here's the example of dataframe1 and dataframe2 这是dataframe1和dataframe2的示例

df1                                       df2
     day     number   id   recordDay         day     number   id    recordDay
2017-03-21     17      1   1990-01-01     2016-03-21    6      2   1991-02-01
2017-03-22     19      1   1990-01-01     2016-03-22    8      2   1991-02-01
2017-03-23     21      1   1990-01-01     2016-03-23   10      2   1991-02-01

Is it possible to save 5 CSV files with file names like this, 'id_1_1991_01_01.csv' , 'id_2_1991_02_01.csv' , 'id_3_1991_03_01.csv' , 'id_4_1991_04_01.csv' , 'id_5_1991_05_01.csv' Or 'id_1.csv' ... 'id_5.csv' would be better? 是否可以保存5个CSV文件,其文件名为'id_1_1991_01_01.csv''id_2_1991_02_01.csv''id_3_1991_03_01.csv''id_4_1991_04_01.csv''id_5_1991_05_01.csv''id_1.csv' 。 .. 'id_5.csv'会更好吗?

I used the following code, but it only saved one CSV file. 我使用了以下代码,但只保存了一个CSV文件。

pd.concat(df_dict).to_csv('data.csv', index = False, data_format = '%Y-%m-%d)

Iterate over the dictionary - using .iloc[] to get the recordID and id values for the name. 遍历字典-使用.iloc []获取名称的recordID和id值。

df1 = pandas.DataFrame(numpy.random.randn(3, 4), columns=[["day", "number", "id", "recordDay"]])
df2 = pandas.DataFrame(numpy.random.randn(3, 4), columns=[["day", "number", "id", "recordDay"]])
df3 = pandas.DataFrame(numpy.random.randn(3, 4), columns=[["day", "number", "id", "recordDay"]])

df_dict={"data_frame1":df1, "data_frame2": df2, "data_frame3": df3}

for name, df in df_dict.items():
    #get the id and recordDay values from each df
    df_id=df['id'].iloc[0]
    df_record_day=df['recordDay'].iloc[0]

    #generate a unique file name based on the id and record
    file_name="id_"+str(df_id)+"_"+str(df_record_day)+".csv"

    #create the CSV
    df.to_csv(file_name, index = False, data_format = '%Y-%m-%d')

Or you can use a list of arrays instead of a dictionary 或者您可以使用数组列表而不是字典

df_list=[df1, df2, df3]

for df in df_list:
    #get the id and recordDay values from each df
    df_id=df['id'].iloc[0]
    df_record_day=df['recordDay'].iloc[0]

    #generate a unique file name based on the id and record
    file_name="id_"+str(df_id)+"_"+str(df_record_day)+".csv"

    #create the CSV
    df.to_csv(file_name, index = False, data_format = '%Y-%m-%d')

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

相关问题 如何在 python 中保存多个文件名不同的文件? - How to save multiple files with different file names in python? 有没有function可以在python中保存多个csv不同名字的文件? - Is there a function to save multiple csv files with different names in python? 如何在 FOR LOOP 中将文件名列表保存到 CSV 文件? - How to save a list of files names to CSV file in a FOR LOOP? Python熊猫使用不同的名称保存多个CSV - Python pandas save multiple CSV with different names 如何读取Python中csv个递增名称的文件并创建不同的对象? - How to read csv files in Python with incremental names and create different objects? 如何执行python程序并自动以不同的文件名保存输出 - How to execute python program and save output in different file names automatically Python:比较CSV文件并保存与第一行的区别(列名) - Python : Compare CSV files and save the difference with first row(Column Names) Python Lambda 函数根据文件名对不同的 CSV 文件执行一些操作 - Python Lambda function to perform some operation on different CSV files depending on file names 如何导入具有排序文件名的csv文件? - How to import csv files with sorted file names? python如何以不同的名称保存视频? - python how to save videos by different names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM