简体   繁体   English

将来自不同文件夹的多个csv文件中的选定列合并到单个csv文件中

[英]Combine selected column from multiple csv files from different folders to a single csv file

This is for creating the final dataframe for my analysis.So I have three kinds of csv files.I want to extract specific columns from file 1 & file 2 and concatenate it to file 3 to get a single csv file. 这是为了创建最终的数据帧以进行分析,因此我有三种csv文件,我想从文件1和文件2中提取特定的列,然后将其串联到文件3中以获得单个csv文件。

I have one folder with the three types of files as subfolders-> that is 3 subfolders These 3 subfolders contain data from different parts of the experiment and are sorted by participant numbers. 我有一个文件夹,其中包含三种类型的文件作为子文件夹->即3个子文件夹。这3个子文件夹包含来自实验不同部分的数据,并按参与者编号排序。 For example for participant 1001,I have in each subfolder a file p1001 and similarly for participant 1002, in each folder I have p1002 and so on.. So for each participant, I have three files of the same name but different folders. 例如,对于参与者1001,我在每个子文件夹中都有一个文件p1001,对于参与者1002同样,在每个文件夹中我都有p1002,依此类推。因此,对于每个参与者,我有三个同名但不同文件夹的文件。 How can I make a single csv file for each partcipant combining the selected columns from the three different subfolders? 如何结合来自三个不同子文件夹的选定列,为每个参与者创建单个csv文件?

import pandas as pd
import os, csv, pdb
import glob

a=[]
base_dir='/Users/...../Desktop/data/'
folders = ('All', 'Choice', 'Choice_S')
pattern = '{}/[{}]/**/filename.csv'.format(base_dir, ''.join(folders))
for filename in glob.glob(pattern):
    filename=pd.DataFrame
    df1=filename[filename['reaction_time']]
    a.append[df1

This was what i tried doing 这就是我尝试做的

I tried to recreate your case with one example 我试图用一个例子来重提你的案子

I generated 3 random files each one with 3 columns and 100 lines and each one in a different folder 我生成了3个随机文件,每个文件有3列和100行,每个文件都放在一个不同的文件夹中

import numpy as np 
import pandas as pd


a = np.random.rand(100,3) 
b = np.random.rand(100,3) 
c = np.random.rand(100,3) 


dataframe1 = pd.DataFrame(a)
dataframe2 = pd.DataFrame(b)
dataframe3 = pd.DataFrame(c)

dataframe1.columns = dataframe2.columns = dataframe3.columns = {"col1","col2","col3"}

dataframe1.to_csv("./1/a.csv")
dataframe2.to_csv("./2/a.csv")
dataframe3.to_csv("./3/a.csv")

Then i read back the csv files, then for each column of each file I combined the lines and stored the result in a dataframe containing the 300 combined lines of the 3 files using pandas.concat with axis = 0 , then i combined the columns using the same function with axis = 1 然后我读回csv文件,然后对每个文件的每一列进行合并,并将结果存储在dataframe中 ,该数据帧使用pandas.concat和axis = 0包含3个文件的300条合并行,然后使用与轴= 1相同的功能

a1 = pd.read_csv("./1/a.csv")
a2 = pd.read_csv("./2/a.csv")
a3 = pd.read_csv("./3/a.csv")



combined_col1 = pd.concat([a1["col1"],a2["col1"],a3["col1"]],axis=0) 
combined_col2 = pd.concat([a1["col2"],a2["col2"],a3["col2"]],axis=0) 
combined_col3 = pd.concat([a1["col3"],a2["col3"],a3["col3"]],axis=0) 

combine_col1_col2 = pd.concat([combined_col1,combined_col2],axis=1) 


combine_col1_col2.to_csv("result.csv")

hope it helps. 希望能帮助到你。

暂无
暂无

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

相关问题 如何组合 Python 中多个文件夹中的多个 CSV 文件? - how to combine multiple CSV files from multiple folders in Python? 将多个CSV文件中的列合并为一个文件,并使用for循环制作多个CSV文件 - Combine columns from several CSV files into a single file and making multiple CSV file with for loop 将不同文件夹中的多个 csv 文件连接到 python 中的一个 csv 文件中 - Concatenate multiple csv files from different folders into one csv file in python 将列添加到多个.csv 文件和文件名,因为您将这些.csv 文件组合成单个 dataframe - Adding a column to multiple .csv files with the file name as you combine those .csv files into a single dataframe 将多个 CSV 文件中的列数据合并到一个 CSV 文件中 - Consolidating column data from number of CSV files into a single CSV file 将多个CSV文件中的列合并为一个文件 - Combine columns from several CSV files into a single file 将来自不同目录路径的多个.csv文件与python合并 - Combine multiple .csv files with python from different directory paths Python:将相同的.csv文件从各个文件夹(每个文件夹有一个.csv文件)复制到一个文件夹中 - Python: Copy identical .csv files from various folders (each folder has one .csv file) into a single folder 从多个csv文件读取和合并数据 - Read and combine data from multiple csv files Python - 从 CSV 文件创建多个文件夹 - Python - Create multiple folders from CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM