Python - 如何保存不同文件名的CSV文件?
Python - How to save CSV files with different file names?
我有 5 个 DataFrame
列 'day', 'number', 'id'、'recordDay',然后我将所有 5 个数据帧放在一个 dictionary
中。我想在 5 个 CSV 文件中保存 5 个数据帧,文件名基于 'id' 和 'recordDay'。这是 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
是否可以保存 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'
会更好?
我用了下面的代码,但是它只保存了一个CSV文件。
pd.concat(df_dict).to_csv('data.csv', index = False, data_format = '%Y-%m-%d)
遍历字典 - 使用 .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')
或者您可以使用数组列表而不是字典
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')
我有 5 个 DataFrame
列 'day', 'number', 'id'、'recordDay',然后我将所有 5 个数据帧放在一个 dictionary
中。我想在 5 个 CSV 文件中保存 5 个数据帧,文件名基于 'id' 和 'recordDay'。这是 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
是否可以保存 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'
会更好?
我用了下面的代码,但是它只保存了一个CSV文件。
pd.concat(df_dict).to_csv('data.csv', index = False, data_format = '%Y-%m-%d)
遍历字典 - 使用 .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')
或者您可以使用数组列表而不是字典
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')