如何合并csv文件并使用python添加标头行? [英] How can I combine csv files and add header rows with python?

查看:72
本文介绍了如何合并csv文件并使用python添加标头行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有50个来自St.Louis Fred的价格指数数据的 csv 文件,每个文件的格式如下:

I have 50 csv files of price index data from St.Louis Fred, the format of each is like this:

我想合并多个csv文件,并向其中添加一行标题以实现以下格式:

And I want to combine multiple csv files and add one more row of header to them to achieve the following format:

所以我可以将数据存储在一个csv文件中,请问有什么方法可以使用Python进行处理吗?

So I can store the data in one csv file, may I know is there any way that I can do it with Python?

推荐答案

重复 DATE 列是没有意义的.除非有特定目的.另外,在合并时,您需要注意特定行上的数据属于同一日期.

The repetition of the DATE column doesn't make sense. Unless there is some specific purpose. Also, while merging you want to be careful that the data on a particular line belongs to the same date.

如果要使用 DATE 作为索引进行合并,并使用 OUTER 方法进行合并,则最好使用熊猫.因此,同一日期的值在同一行.

Its better to use pandas if you are merging using DATE as the index and merge using OUTER method. So, the values from the same date are on the same lines.

import pandas as pd;

df1 = pd.read_table('file1.csv', sep=',')
df2 = pd.read_table('file2.csv', sep=',')
df3 = pd.read_table('file3.csv', sep=',')

因此,基本上将所有具有的文件加载为数据框.然后使用 merge reduce 函数合并文件.

So, basically load all the files you have as data frame. Then merge the files using merge or reduce function.

data_frames = [df1, df2, df3]

您可以在上面的代码中添加尽可能多的数据框.

然后合并它们.要保留属于同一日期的值,您需要在 DATE

Then merge them. To keep the values that belong to the same date you need to merge it on the DATE

df_merged = reduce(lambda  left,right: pd.merge(left,right,on=['DATE'],
                                            how='outer'), data_frames)

然后将合并的数据写入csv文件.

Then write the merged data to the csv file.

pd.DataFrame.to_csv(df_merged, 'merged.txt', sep=',', na_rep='.', index=False)

这应该给你

DATE VALUE1 VALUE2 VALUE3 ....

这篇关于如何合并csv文件并使用python添加标头行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆