简体   繁体   English

将多个 csv 文件按列与 header 作为文件名组合

[英]Combine multiple csv files column wise with the header as file name

I couldn't find the exact code on the platform which is why posting it for suggestions.我在平台上找不到确切的代码,这就是为什么发布它以征求建议。

I have multiple CSV files (about 100) with the same data formats and header names.我有多个 CSV 文件(大约 100 个)具有相同的数据格式和 header 名称。

 ,Mean,SD
1,96.432,13.899
2,96.432,13.899
3,96.432,13.899
4,96.432,13.899
5,96.432,13.899

I want to append all files column-wise so that I have them in one file.我想按列对所有文件进行 append ,以便将它们放在一个文件中。 Also, the header of each data should be the file name so that I can follow which data belongs to which file.另外,每个数据的 header 应该是文件名,以便我可以跟踪哪些数据属于哪个文件。 For example, above mean, sd--> another row of the file name.比如上面的意思,sd-->文件名的另一行。

Please guide me, as I am new to Python.请指导我,因为我是 Python 的新手。

Thank you and regards, Khan.谢谢你和问候,汗。

The question was vague about formatting, so this may vary from the desired output.问题是关于格式的模糊,所以这可能与所需的 output 不同。

filenames = [...]
dfs = []
for f in filenames:
    newdf = pd.read_csv(f)
    newdf.rename(columns={'Mean': 'Mean ' + f, 'SD': 'SD ' + f})
    dfs.append(newdf)
df = pd.concat(dfs)

You can use pandas to read and concatenate the files, together with glob and a dictionary comprehension:您可以使用pandas以及glob和字典理解来读取和连接文件:

from glob import glob
import pandas as pd

files = glob('/tmp/*.csv') # change the location/pattern accordingly
# if you have a list of files, use: files=['file1.csv', 'file2.csv'...]

df = pd.concat({fname.rsplit('/')[-1]: pd.read_csv(fname, index_col=0)
                for fname in files}, axis=1)

output: output:

>>> print(df)
  file1.csv         file2.csv        
       Mean      SD      Mean      SD
                                     
1    96.432  13.899    96.432  13.899
2    96.432  13.899    96.432  13.899
3    96.432  13.899    96.432  13.899
4    96.432  13.899    96.432  13.899
5    96.432  13.899    96.432  13.899

Saving to new file:保存到新文件:

df.to_csv('concatenated_file.csv')

output: output:

,file1.csv,file1.csv,file2.csv,file2.csv
,Mean,SD,Mean,SD
 ,,,,
1,96.432,13.899,96.432,13.899
2,96.432,13.899,96.432,13.899
3,96.432,13.899,96.432,13.899
4,96.432,13.899,96.432,13.899
5,96.432,13.899,96.432,13.899

you can use pandas to work with您可以使用 pandas 与

In [3]: import pandas  

                                                                                                                           

In [4]: import pandas as pd                                                                                                                           

In [13]: ls                                                                                                                                           
abc1.csv  abc.csv

In [14]: df = pd.read_csv('abc.csv')                                                                                                                  

In [15]: df1 = pd.read_csv('abc1.csv')                                                                                                                

In [16]: df                                                                                                                                           
Out[16]: 
        Mean      SD
0  1  96.432  13.899
1  2  96.432  13.899


In [16]: df                                                                                                                                           
Out[16]: 
        Mean      SD
0  1  96.432  13.899
1  2  96.432  13.899

In [17]: df1                                                                                                                                          
Out[17]: 
        Mean      SD
0  3  96.432  13.899
1  4  96.432  13.899
2  5  96.432  13.899

In [18]: df.append(df1)                                                                                                                               
Out[18]: 
        Mean      SD
0  1  96.432  13.899
1  2  96.432  13.899
0  3  96.432  13.899
1  4  96.432  13.899
2  5  96.432  13.899

In [19]: ds = df.append(df1)                                                                                                                          

In [20]: ds                                                                                                                                           
Out[20]: 
        Mean      SD
0  1  96.432  13.899
1  2  96.432  13.899
0  3  96.432  13.899
1  4  96.432  13.899
2  5  96.432  13.899



In [21]: ds.to_csv('file1.csv')  


In [23]: ls                                                                                                                                           
abc1.csv  abc.csv  file1.csv

暂无
暂无

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

相关问题 将列添加到多个.csv 文件和文件名,因为您将这些.csv 文件组合成单个 dataframe - Adding a column to multiple .csv files with the file name as you combine those .csv files into a single dataframe 明智地按列组合文本文件 - combine text files column wise 在一个标题下按列附加到 csv 文件 - Append to csv file column-wise under one header 将多个 CSV 文件与相同的 Header 合并到不同的组文件中 - Combine multiple CSV files with Same Header into different group files 如何将共享同一列的多个 CSV 文件合并到一个新的 CSV 文件中? - How can I combine multiple CSV files that share an identical column into a new CSV file? 将来自不同文件夹的多个csv文件中的选定列合并到单个csv文件中 - Combine selected column from multiple csv files from different folders to a single csv file 根据一列内的匹配项合并2个CSV文件,而不管标题行 - Combine 2 CSV files based on a match within a column disregarding the header row 合并多个csv文件 - Combine multiple csv files 我正在尝试将多个 csv 文件(13 个文件)合并为一个(按列),但我得到的结果不是按列进行的 - I am trying to concatinate multiple csv files (13 files) into one (column wise) and the results i am getting is not coming column wise 导入多个CSV文件,从每个文件中选择1列,然后用Jupyter Noteboos中的文件名重命名该列 - Import multiple CSV files, select 1 Column from each file & rename the column with the file name in Jupyter Noteboos
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM